c语言统计字符串中指定的单词个数是几个?

2025-01-01 19:59:19
推荐回答(1个)
回答1:

#include
#include
int search_string(char *target,char *search)
//target可以为"asgeage ghelloae gegahellosd hell oage hello"
//search可以为"hello"
{
bool judge = true;
int n = 0;
for(int i = 0; i < strlen(target); i++)
{
judge = true;
for(int j = 0; j < strlen(search); j++)
if(target[i+j] != search[j])
{
judge = false;
break;
}
if(judge)
n++;
}
return n;
}
void main(void)
{
char target[400] = {0},search [400] = {0};//
printf("输入字符串:\n");
gets(target);
printf("输入要查找的字符串\n");
scanf("%s",search);
printf("出现的次数为:%d\n",search_string(target,search));
}

不懂再问哈