c语言代码实现:
-
#include<stdio.h>
-
#include<string.h>
-
int searchnum(char *str,char *pattern)
-
{
-
if (str == NULL)
-
return 0;
-
char *pos = NULL;
-
int count = 0;
-
while ((pos = strstr(str,pattern)) != NULL)
-
{
-
count++;
-
pos += (strlen(pattern));
-
str = pos;
-
// count = count+1;
-
}
-
return count;
-
}
-
int main()
-
{
-
char *str = "world hello world word world";
-
printf("%d\n",searchnum(str,"world"));
-
return 0;
-
}
运行结果:
[root@localhost ~]# ./a.out
3
阅读(1405) | 评论(0) | 转发(0) |