题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
http://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include <stdio.h>
#include <string.h>
//获取所有数字子串及其长度并存储
int main()
{
char str[200];
while (scanf("%s", str) != EOF)
{
int strlength = strlen(str);
char subNumStr[200][200]={0};
int len[200];
int top = -1;
int start = 0, end = 0;
while (end < strlength)
{
while (str[start] < '0' || str[start] > '9')
{
start++;
end++;
}
while (str[end] >= '0' && str[end] <= '9')
{
end++;
}
strncpy(subNumStr[++top], str + start, end - start);
len[top] = end - start;
start = end;
}
int max = 0;
for (int i = 0; i <= top; i++)
if (len[i] > max)
max = len[i];
for(int i=0;i<=top;i++){
if(len[i]==max)
printf("%s",subNumStr[i]);
}
printf(",%d\n",max);
}
return 0;
}