求最大连续递增数字串(如"ads3sl456789DF3456ld345AA"中的"456789")
编辑: 面试啦 发布时间: 2023-09-06 05:20:58
int GetSubString(char *strSource, char *strResult) {
int iTmp=0, iHead=0, iMax=0;
for(int Index=0, iLen=0; strSource[Index]; Index++) {
if(strSource[Index] >= ’0′ && strSource[Index] <= ’9′ &&
strSource[Index-1] > ’0′ && strSource[Index] == strSource[Index-1]+1) {
iLen++; // 连续数字的长度增1
} else { // 出现字符或不连续数字
if(iLen > iMax) {
iMax = iLen; iHead = iTmp;
}
// 该字符是数字,但数字不连续
if(strSource[Index] >= ’0′ && strSource[Index] <= ’9′) {
iTmp = Index;
iLen = 1;
}
}
}
for(iTmp=0 ; iTmp < iMax; iTmp++) // 将原字符串中最长的连续数字串赋值给结果串
strResult[iTmp] = strSource[iHead++];
strResult[iTmp]=’\0′;
return iMax; // 返回连续数字的最大长度
}
int main(int argc, char* argv[]) {
char strSource[]=”ads3sl456789DF3456ld345AA”, char strResult[sizeof(strSource)];
printf(“Len=%d, strResult=%s \nstrSource=%s\n”,
GetSubString(strSource, strResult), strResult, strSource);
}
int iTmp=0, iHead=0, iMax=0;
for(int Index=0, iLen=0; strSource[Index]; Index++) {
if(strSource[Index] >= ’0′ && strSource[Index] <= ’9′ &&
strSource[Index-1] > ’0′ && strSource[Index] == strSource[Index-1]+1) {
iLen++; // 连续数字的长度增1
} else { // 出现字符或不连续数字
if(iLen > iMax) {
iMax = iLen; iHead = iTmp;
}
// 该字符是数字,但数字不连续
if(strSource[Index] >= ’0′ && strSource[Index] <= ’9′) {
iTmp = Index;
iLen = 1;
}
}
}
for(iTmp=0 ; iTmp < iMax; iTmp++) // 将原字符串中最长的连续数字串赋值给结果串
strResult[iTmp] = strSource[iHead++];
strResult[iTmp]=’\0′;
return iMax; // 返回连续数字的最大长度
}
int main(int argc, char* argv[]) {
char strSource[]=”ads3sl456789DF3456ld345AA”, char strResult[sizeof(strSource)];
printf(“Len=%d, strResult=%s \nstrSource=%s\n”,
GetSubString(strSource, strResult), strResult, strSource);
}
词条:c面试题
上一篇:C有"按引用传递"吗 下一篇:求高于平均分的学生学号及成绩
面试题库导航
- C++面试题
- JAVA面试题
- Oracle面试题
- MySQL面试题
- Linux面试题
- 网络工程师面试题
- 网管面试题
- 软件工程师面试题
- PHP面试题
- .net面试题
- 软件测试面试题
- EJB面试题
- Spring面试题
- Javascript面试题
- Python面试题
- QTP面试题
- Servlet面试题
- J2EE面试题
- Database面试题
- ASP面试题
- Unix面试题
- Loadrunner面试题
- 通信面试题
- 嵌入式面试题
- Ruby面试题
- Delphi面试题
- Mobile开发面试题
- SOA面试题
- JAVA面试题面试题
- 研发工程师面试题
- 软件架构师面试题
- 系统工程师面试题
- Android面试题
- JAVA程序员
- C#面试题