您的位置:首页 > 编程语言 > C语言/C++

求一个字符串中的最大长度的数字

2013-04-16 21:13 330 查看
#include<cstdio>
#include<cstdlib>
#include<iostream>
#include<cstring>
using namespace std;
int GetMaxLenNumber(char *inputStr,int len)
{
//开始处理的位置
int startPos = 0;
//当前处理的字符长度
int tempCharCount = 0;
//数字的最长长度
int maxLen = 0;

int pos = 0;
while (startPos < len)
{
//循环中的临时最大长度
int tempMax = 0;
while (tempCharCount + startPos < len)
{
//开始处理的字符
char c = inputStr[tempCharCount + startPos];
if (isdigit(c))
{
//如果是数字
tempMax++;
if (tempMax > maxLen)
{
maxLen = tempMax;
pos = startPos;
}
}
else
{
//不是数字
tempMax = 0;
startPos++;
break;
}
tempCharCount++;
}
if (startPos + tempCharCount == len)
{
break;
}
tempCharCount = 0;
}

return maxLen;
}
int main()
{
char a[]="654359006 uestc 18224001830 get the longest digit string!";
puts(a);
cout<<"GetMaxLenNumber="<<GetMaxLenNumber(a,sizeof(a)/sizeof(a[0]))<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签:  CC++ 笔试面试
相关文章推荐