求两个字符串最大的公共字串的长度
2013-11-26 22:36
253 查看
//2013-11-26 22:10 #include <stdio.h> #include <string.h> //转化成小写 void mytolower(char* strSrc) { while ( *strSrc != '\0') { if ( *strSrc >= 'A' && *strSrc <= 'Z') { *strSrc = *strSrc +32; } strSrc ++; } } // int GetCurCommonStrLength(char* strFirst,char* strSecond) { int nLength = 0; while ( *strFirst == *strSecond) { nLength++; strFirst ++; strSecond++; } return nLength; } //求两个字符串的最大公共字符串的长度,不考虑字符串是否大小写 //比如“abcdefghijk”和“bcaabcdedd”返回5 int GetCommonStrLength(char* pStrFirst,char* pStrSecond) { if ( NULL == pStrFirst || NULL == pStrSecond) { return 0; } if ( pStrFirst == "" || pStrSecond == "") { return 0; } char strFirsttemp[512] = {0}; char strSecondtemp[512] = {0}; strcpy(strFirsttemp,pStrFirst); strcpy(strSecondtemp,pStrSecond); mytolower(strFirsttemp); mytolower(strSecondtemp); char* strFirst = strFirsttemp; char* strSecond = strSecondtemp; int nCommonLength = 0; while ( *strFirst != '\0') { while ( *strSecond != '\0') { if ( *strFirst == *strSecond) { int nTempCommon = GetCurCommonStrLength(strFirst,strSecond); if ( nTempCommon > nCommonLength) { nCommonLength = nTempCommon; } } strSecond++; } strSecond = strSecondtemp; strFirst++; } return nCommonLength; } int main() { char* str1 = "abcdefghijk"; char* str2= "bcaabcdedd"; char* str3 = "xxp520jh"; char* str4 = "jh520xp"; int n = GetCommonStrLength(str3,str4); return 0; }
相关文章推荐
- 求两个字符串最大的公共字串的长度
- 题目标题: 计算两个字符串的最大公共字串的长度,字符不区分大小写
- 华为机试:计算两个字符串的最大公共字串的长度,字符不区分大小写
- 计算两个字符串的最大公共字串的长度,字符不区分大小写(要求计算连续最长字串的长度)
- 求两个字符串的最大连续公共字串
- 求两个字符串的最大公共长度 看到的一道阿里笔试题
- 求两个字符串的最大公共字串
- 求两个字符串的最大连续公共字串
- 求两个字符串公共子序列的最大长度(参考阿里巴巴2015研发笔试)(简单)
- C语言实现寻找两个字符串中的最大公共字符串长度
- 计算两个字符串的最大公共字符串的长度,字符不区分大小写
- java_基础知识_字符串练习题_计算两个字符串的最长公共字串长度
- java 温习之字符串(获取两个字符串的最大公共字串)
- 求两个字符串的公共子串的最大长度
- 两个字符串中最大的公共字串
- 华为OJ平台 求2个字符串的最大公共字串长度
- Java语言实现查找两个字符串的最大公共字串
- (Relax 后缀数组1.4)POJ 2774 Long Long Message(求两个字符串公共子串的最大长度)
- 2015年阿里在线笔试题:求两个字符串的最大公共子序列长度的C语言解法
- 求两个字符串的最大公共字串