返回两个字符中的最长公共子串

来源:互联网 发布:淘宝网 花种子 编辑:程序博客网 时间:2024/06/05 10:48

1,输入:abncdeixox dixcdeixobxg

     输出:cdeixo

    

     代码如下:

  

#include "stdafx.h"#include<stdio.h>#include<string.h>#include<malloc.h>#define  MAXSIZE 100int SearchStr(char * pFirstStr,  const char * pSecondStr){int lenS;char*p;lenS=strlen(pSecondStr);if(pSecondStr!=NULL){p=strstr(pFirstStr,pSecondStr);if(p)return lenS;}return 0;}void commonStrLength(char*pFirstStr,char*pSecondStr){int i,j,lenS;int length;int maxLength=0;char*temp;char*p;char*final;temp=(char*)malloc(MAXSIZE*sizeof(char));final=(char*)malloc(MAXSIZE*sizeof(char));lenS=strlen(pSecondStr);for(i=0;i<lenS;i++){p=pSecondStr+i;for(j=1;j<=lenS-i;j++){strncpy(temp,p,j);*(temp+j)='\0';length=SearchStr(pFirstStr,temp);if(length>maxLength){strcpy(final,temp);maxLength=length;}}}//printf("%d\n",maxLength);printf("%s\n",final);//return maxLength;}/*char*strBtob(char*ch){char*p;p=ch;if(ch==NULL)return NULL;while(*p!='\0'){if(*p>='A'&&*p<='Z')*p=(*p)+32;p++;}return ch;}*/void main(){int len;char *firstCh;char *secondCh;firstCh=(char*)malloc(100);secondCh=(char*)malloc(100);    scanf("%s %s",firstCh,secondCh);//strlwr(firstCh);//strlwr(secondCh);commonStrLength(firstCh,secondCh);//printf("%d\n",len);}

运行结果:


0 0
原创粉丝点击