#include
#include
#include
int is_within(char*pst, char* ch);
int main()
{
char*pst = "fufufufck";
char* ch = "u";
printf("%d",is_within(pst, ch));
system("pause");
return 0;
}
int is_within(char*pst, char* ch)
{
int length = strlen(pst);
int i = 0;
int j = 0;
while (i++ < length)
{
if (*ch == *pst++)
{
j = 1;//return 1;
break;
}
else j = 0;//return 0;
}
return (j); //去掉
}
一个查找字符功能的函数,如原码是返回1,但是如果用注释内的return则会返回0。
如果用return的话该怎么做呢
你注释中的return 0放的位置不对,应该放到return j那行去,要不只判断了第一个字符就会返回,当然返回的是0(除非第一个字符就是)
除非直到最后,否则找不到不能返回0,还得继续找。
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
int is_within(char*pst, char* ch);
int main()
{
char*pst = "fufufufck";
char* ch = "u";
printf("%d",is_within(pst, ch));
system("pause");
return 0;
}
int is_within(char*pst, char* ch)
{
int length = strlen(pst);
int i = 0;
int j = 0;
while (i++ < length)
{
if (*ch == *pst++)
{
return 1;
break;
}
else if (i == length) return 0;
}
}