求子串个数(C语言)

来源:互联网 发布:php方面的书籍 编辑:程序博客网 时间:2024/06/05 02:09

在任意输入两个字符串,第二个作为子串,检查第一个字符串中含有几个这样的子串。

C语言实现:

#include “stdio.h"

#include "string.h"

#include "stdlib.h"

void main()
{
 char *s1;
 char *s2;
 int len1,len2,i=0,j=0,count=0;
 s1=(char *)malloc(sizeof(char)*100);
 s2=(char *)malloc(sizeof(char)*100);
 scanf("%s",s1);
 scanf("%s",s2);
 len1=strlen(s1);
 len2=strlen(s2);
    while(i<len1)                                               
 {
  if(s1[i]==s2[j])                                           
  {
   i++;
   j++;
  }
  else
  {
   i=i-j+1;
   j=0;
  }
  if(j==len2)
  {  count++;
   j=0;
  }
 }
 printf("%d",count);
}


1 0
原创粉丝点击