题目10;子串出现次数
八个英文长句,事先存放于字符数组中。输入一个单词判断该单词在长句中出现了几次。区分大小写。
如: In the _end, it's_ not the _years in your. life _that _count. (表示空格输入: the, 输出: the 在本句出现了2次。
输入: THE, 输出: THE 在本句出现了0次。
题目10;子串出现次数
八个英文长句,事先存放于字符数组中。输入一个单词判断该单词在长句中出现了几次。区分大小写。
如: In the _end, it's_ not the _years in your. life _that _count. (表示空格输入: the, 输出: the 在本句出现了2次。
输入: THE, 输出: THE 在本句出现了0次。
#include <stdio.h>
#include <string.h>
int main(void)
{
const char *str = ": In the _end, it's_ not the _years in your. life _that _count.", *pstr;
char substr[32];
int cnt = 0;
scanf("%s", substr);
//len = strlen(substr);
pstr = strstr(str, substr);
while (pstr) {
cnt++;
pstr = strstr(pstr+1, substr);
}
printf("cnt = %d\n", cnt);
return 0;
}
供参考~
尝试使用strstr函数~