#include <stdio.h> #include <stdlib.h> #include <string.h> #include <iostream> using namespace std; void MaxStr(char* str1, char* str2,char* sam) { if (str1 == NULL || str2 == NULL) { return; } int nLen1,nLen2,pos1,pos2; nLen1 = strlen(str1); nLen2 = strlen(str2); int count, nMax=0; int i,j; for (i=0;i<nLen1;i++) { for (j=0;j<nLen2;++j) { if (str1[i] == str2[j]) { pos1 = i; pos2 = j; count = 0; cout<<"i:"<<i<<" j:"<<j<<endl; while((str1[pos1]==str2[pos2])&& (pos1<nLen1 && pos2<nLen2)) { count++; pos1++; pos2++; } //end while cout<<"count:"<<count<<endl; if (count>nMax) { nMax = count; memcpy(sam,&str2[j],nMax); }//end if cout<<endl; }//end str[i]==str2[j] }//end j }//end i } int main() { char* str1 = "zabcd"; char* str2 = "nzgababcabcdfd"; char samStr[20] = {0}; MaxStr(str1,str2,samStr); cout<<samStr<<endl; return 0; }