编写函数 bool IsSubstring(char*str,char*substr) 功能是判断字符串substr在字符串str中是否出现

2024-12-16 05:30:49
推荐回答(1个)
回答1:

# include
# include
# define bool int
# define true 1
# define false 0
bool IsSubstring(char *str, char * substr);
int main(void)
{
int state;
char *str = "Merry Christmas";
char *substr = "stmas";
state = IsSubstring(str, substr);
if(state)
printf("字符串%s包含在字符串%s中。\n", substr, str);
else
printf("字符串%s不包含在字符串%s中。\n", substr, str);
return 0;
}
bool IsSubstring(char *str, char * substr)
{
int len;
char *ptr = (char*)malloc(sizeof(str));
strcpy(ptr, str);
len = strlen(substr);
while(strncmp(ptr, substr,len) != 0 && (*ptr!= '\0'))
{
ptr++;
}
if(*ptr == '\0')
return false;
else
return true;
}

如果是在visual C++ 6.0里面编译,可以去掉第3-5行。