字符串匹配之返回子串下标及出现次数

/*
 * 任务描述:给定一个长字符串和一个短字符串,找出短串在长串中出现的次数,
 * 并输出每次长串中出现短串的起始位置。(可用字符串相关函数简化编程工作)

 */
#include<stdio.h>
#include <string.h>
#define MAX 500
int  main()
{
   char moch[MAX],soch[MAX];
   int i,j,k,count=0;
   puts("input the mother chars:");
   gets(moch);
   puts("input the child chars");
   gets(soch);
   int molen=strlen(moch),
   solen=strlen(soch);
   for( i=0;i<molen-solen+1;i++){
       for( j=i,k=0;moch[j]==soch[k]&&k<solen;k++,j++){
     if(!soch[k+1]){
         count++;
         printf("p=%d
",j-1);
     }
       }

   }
   printf("%d",count);
}
原文地址:https://www.cnblogs.com/jeseesmith/p/12724887.html