zoukankan      html  css  js  c++  java
  • 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;
     
    }
  • 相关阅读:
    shell 时间循环
    t
    IntelliJ IDEA For Mac 快捷键
    JVM的默认参数
    qt不同模块使用多语言
    cocos2dx 实现gpu instancing
    so so.*.*
    Android开发-解决 AIDL 中找不到couldn't find import for class错误
    Android Watchdog源码简析--Based on Android 6.0.1
    View绘制流程--Based on kitkat
  • 原文地址:https://www.cnblogs.com/0515offer/p/4556526.html
Copyright © 2011-2022 走看看