#include <stdio.h> #include <windows.h> int main() { char a[], b[]; char *temp; int counter = ; memset( a, sizeof(a), ); //清空内存 memset( a, sizeof(b), ); //清空内存 printf( "Please input source string: " ); gets(a); //从缓冲区获取源字符串. printf( "Please input find string: " ); gets(b); //从缓冲区获取查找字符串. temp = a; //将源字符串赋给指针操作. while( temp ) { temp = strstr( temp, b ); //在源字符串中查找
//第一次出现的位置,找到返回所处位置,未找到返回NULL.
if( temp != NULL ) //如果能找到
//,指针偏移查找字符串的长度,然后继续循环,直到查找完成.
{ temp += strlen(b); counter++; } } printf( "find %d times!\n", counter ); //输出找到的个数. system("pause"); return ; }
#include <stdio.h>
#include <string.h> int main()
{ int findCount(char *str1,char const*str2); printf("%d",findCount("","")); return ;
} /**
查找str1中str2出现的次数
*/
int findCount(char *str1,char const*str2)
{ int count=;
char *ptmp=str1; while((ptmp=strstr(ptmp,str2))!=NULL)
{
ptmp+=strlen(str2);
count++;
} return count; }