1.描述
计算两个字符串的最大公共字串的长度,字符不区分大小写。
2.原型
int getCommonStrLength(char * pFirstStr, char * pSecondStr);
3.输入参数
char * pFirstStr //第一个字符串
char * pSecondStr//第二个字符串
4.样例输入
asdfas werasdfaswer
5.样例输出
6
6.代码实现
#include<stdio.h>
#include<string.h>
int getCommonStrLength(char* pFirstStr, char* pSecondStr)
{
char temp[1000];
if(pFirstStr == NULL || pSecondStr == NULL)
exit(1);
int FirstLen = strlen(pFirstStr);
int SecondLen = strlen(pSecondStr);
int i, j, k, m, z, Len, max = 0;
if(FirstLen > SecondLen) // 让FirstLen始终是最短的
{
Len = FirstLen;
FirstLen = SecondLen;
SecondLen = Len;
strcpy(temp, pFirstStr);
strcpy(pFirstStr, pSecondStr);
strcpy(pSecondStr, temp);
}
for(i = 0; i < FirstLen; i++)
{
for(j = 0; j < SecondLen; j++)
{
if(*(pFirstStr + i) == *(pSecondStr + j) || *(pFirstStr + i) == *(pSecondStr + j) + 'A' - 'a'
|| *(pFirstStr + i) == *(pSecondStr + j) - 'A' + 'a')
{
k = 0;
m = i;
z = j;
while (*(pFirstStr + m) == *(pSecondStr + z) || *(pFirstStr + m) == *(pSecondStr + z) + 'A' - 'a'
|| *(pFirstStr + m) == *(pSecondStr + z) - 'A' + 'a')
{
k++;
m++;
z++;
if(m >= FirstLen || z >= SecondLen)
{
break;
}
}
if(k > max)
{
max = k;
}
}
}
}
printf("%d\n", max);
return 0;
}
int main()
{
char FirstStr[1000], SecondStr[1000];
while(scanf("%s %s", FirstStr, SecondStr) != EOF)
{
getCommonStrLength(FirstStr, SecondStr);
}
return 0;
}
个人主页:
网友评论