找出两个字符串的最大公串!例如,“adbccadebbca”和“edabccadece”,返回“ccade”
我的运行结果不对,找不到错误,请大家帮助一下.
#include <iostream>
#include <string.h>
using namespace std;
int main()
{
char a[20];
char b[20];
cout<<"please enter string1:"<<endl;
cin>>a;
cout<<"please enter string2:"<<endl;
cin>>b;
int size1=strlen(a);
int size2=strlen(b);
int size=size1>size2?size2:size1;
char *newstring=new char(size+1);
for(int i=0;i<size1;i++)
{
for(int j=0;j<size2;j++)
{
if(a[j]==b[i])
{
int count=0;
while(a[j]!=b[i])
{
j++;
i++;
count++;
}
int max=0;
if(count>max)
{
max=count;
strncpy(newstring,a+j,max);
}
count=0;
}
}
}
cout<<newstring<<endl;
return 0;
}
关于字符串的问题