C 语言 查找一个字符串2在字符串1中出现的次数

#include <stdio.h>
 
#include <windows.h>
 
int main()
 
{
 
   char a[100], b[100];
 
   char *temp;
 
   int counter = 0;
 
   memset( a, sizeof(a), 0 ); //清空内存
  
   memset( a, sizeof(b), 0 ); //清空内存
       
   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!
", counter ); //输出找到的个数.
  
   system("pause");
 
   return 0;
 
}
#include <stdio.h>
#include <string.h>
 
 
int main()
{
 
    int findCount(char *str1,char const*str2);
 
 
    printf("%d",findCount("123412","12"));
 
  
    return 0;
}
 
/**
查找str1中str2出现的次数
*/
int findCount(char *str1,char const*str2)
{
 
    int count=0;
    char *ptmp=str1;
 
    while((ptmp=strstr(ptmp,str2))!=NULL)
    {
       ptmp+=strlen(str2);
        count++;
    }
 
    return count;
 
}
原文地址:https://www.cnblogs.com/0515offer/p/4556526.html