编写函数查找字符串中子字符串出现的次数

来源:互联网 发布:淘宝信用借贷额度 编辑:程序博客网 时间:2024/06/04 23:01

题目:请编写一个函数,完成在一个字符串中查找子字符出现的次数。例如:在字符串“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;}


运行结果:


0 0
原创粉丝点击