运行结果和预想的不一样~
题目:编写程序,在两个已知的字符串中查找所有非空的最长公共子串的长度与个数!#include <stdio.h>
#include <string.h>
#define maxline 120
void main(void)
{char *str1,*str2,*tstr;
int len1,len2,k,p,i,slen,count;
str1=(char *) malloc(maxline);
str2=(char *) malloc(maxline);
if(!str1||!str2)
{printf("memory allocation error!\n");
exit(0);
}
printf("input first string:");
gets(str1);
printf("input second string:");
gets(str2);
len1=strlen(str1);
len2=strlen(str2);
if(len1>len2)
{tstr=str1;str1=str2;str2=tstr;slen=len1;len1=len2;len2=slen;}
for(slen=len1;slen>0;--slen)
{for(k=0;k+slen<=len1;++k)
{for(p=0;p+slen<len2;++p)
{for(i=0;i<slen;++i)
if(str1[k+i]!=str2[p+i]) break;
if(i==slen)
count++;
}
}
if(count) break;
}
printf("numbers of substring:%d,length of substring:%d",count,slen);
getche();
}