题目:请编写一个函数,完成在一个字符串中查找子字符出现的次数。例如:在字符串“President Obama has announced his support for Indio's bid for a permanent place on the United Nation Security Council.Obama was addressing the Indian parliament”中查找“Obama”出现的次数。
代码:
#include <stdio.h>
#include <string.h>
int main()
{
int i=0;
char *str1="President Obama has announced his support for Indio's bid for a permanent place on the United Nation Security Council.Obama was addressing the Indian parliament.",*str2="Obama",*ptr;
ptr=strstr(str1,str2);
while(ptr)
{
i++;
ptr+=strlen(str2);
ptr=strstr(ptr,str2);
}
printf("The number of substring is:%d\n",i);
return 0;
}
运行结果: