strins-func strstr- func dosent返回NULL [英] strins-func strstr- the func dosent return NULL
本文介绍了strins-func strstr- func dosent返回NULL的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
你好,我使用func''strstr''并且由于某种原因它不会返回NULL,即使两个字符串完全不同,这是我的工作:
#include < stdio.h >
#include < conio.h >
#include < string.h >
#include < malloc.h >
#define N 100
void replaceSubstring( char * str, char * sub_str);
int main()
{
int flag = 1 ,flag2 = 1 ;
char str [N],sub_str [N],i;
char * pos,* pos2;
while (flag)
{
printf( 输入文字:\ n);
得到(str);
if (str [ 0 ] == 0 )
{
flag = 0 ;
printf( \ nFINISH);
free(str);
free(sub_str);
getch();
return 0 ;
}
else
{
printf( \ n输入子字符串:\ n);
得到(sub_str);
replaceSubstring(str,sub_str);
}
}
}
void replaceSubstring( char * str, char * sub_str)
{
char * pos;
int flag2 = 1 ,i;
while (flag2)
{
pos = strstr(str,sub_str);
if (pos!= NULL)
{
for ( i = 0 ; sub_str [i]> 0; i ++)
{
pos [i] - = 32 跨度>;
}
}
其他
{
flag2 == 0 跨度>;
}
}
}
已添加代码块[/ Edit]
解决方案
< pre lang =c ++> else
{
flag2 == 0 ;
}
问题是你从未将flag2
设置为零。上面的行是一个布尔表达式,它返回值false
但不更改标志。如果应该是:
else
{
flag2 = 0 跨度>;
}
Hello, im using the func ''strstr'' and for some reason it doesnt return NULL even though the two strings are completely diffrent, here is my work:
#include<stdio.h>
#include<conio.h>
#include<string.h>
#include<malloc.h>
#define N 100
void replaceSubstring(char *str, char *sub_str);
int main()
{
int flag=1,flag2=1;
char str[N],sub_str[N],i;
char *pos,*pos2;
while(flag)
{
printf("Enter text : \n");
gets(str);
if(str[0]==0)
{
flag=0;
printf("\nFINISH");
free(str);
free(sub_str);
getch();
return 0;
}
else
{
printf("\nEnter substring : \n");
gets(sub_str);
replaceSubstring(str,sub_str);
}
}
}
void replaceSubstring(char *str, char *sub_str)
{
char *pos;
int flag2=1,i;
while(flag2)
{
pos=strstr(str,sub_str);
if(pos != NULL)
{
for(i=0;sub_str[i]>0;i++)
{
pos[i]-=32;
}
}
else
{
flag2==0;
}
}
}
[Edit]Code block added[/Edit]
解决方案
else { flag2==0; }
The problem is that you never setflag2
to zero. The line above is a boolean expression which returns the valuefalse
but does not change the flag. If should be:
else { flag2 = 0; }
这篇关于strins-func strstr- func dosent返回NULL的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文