1121

来源:互联网 发布:youtube网络连接错误 编辑:程序博客网 时间:2024/05/17 20:29

 输入一个字符串,计算字符串中子串出现的次数

  1. #include <stdio.h>  
  2. #include <string.h>  
  3. void my_fun(char a[],char b[])  
  4. {  
  5.    int i = 0;  
  6.    int j = 0;  
  7.    int count=0;  
  8.    int n = 0;  
  9.    int num = 0;  
  10.    n = strlen(b);  
  11.    while(a[i]!='\0')  
  12.    {  
  13.        if(a[i]==b[j])  
  14.        {  
  15.           count++;  
  16.       j++;  
  17.       i++;  
  18.       if((count%n)==0)  
  19.       {  
  20.          num++;  
  21.          j = 0;  
  22.       }  
  23.        }  
  24.        else if(count!=0)  
  25.        {  
  26.           count = 0;  
  27.       j = 0;  
  28.        }  
  29.        else  
  30.        {  
  31.           i++;  
  32.        }  
  33.    }  
  34.    printf("字串个数为:%d\n",num);  
  35. }  
  36. int main()  
  37. {  
  38.    char a[20];  
  39.    char b[5];  
  40.    printf("请输入一个字符串(20个字节以内):");  
  41.    scanf("%s",a);  
  42.    printf("请输入子串(5个字节以内:)");  
  43.    scanf("%s",b);  
  44.    my_fun(a,b);  
  45.    return 0;  
  46. }  

0 0
原创粉丝点击