题解 | #在字符串中找出连续最长的数字串#
在字符串中找出连续最长的数字串
https://www.nowcoder.com/practice/2c81f88ecd5a4cc395b5308a99afbbec
#include<stdio.h>
#include<string.h>
int main()
{
char str[201];
int i, j, max, index = 0;
while(scanf("%s", str) != EOF)
{
i = 0;
max = 0;
while((i < strlen(str)) && (str[i] != '\n'))//i遍历字符串
{
if(str[i] >= '0' && str[i] <= '9')//找到数字串的起始点
{
j = i;
while((i < strlen(str)) && (str[i] >= '0') && (str[i] <= '9'))//求数字串的长度
i++;
if(i - j > max)//若长度长于已记录的最长长度,将数字串写入覆盖原来的字符串
{
max = i - j;
index = 0;
while(j < i)
str[index++] = str[j++];
}
else
if(i - j == max)//若长度和已记录的最长长度相同,将数字串续写入原来的字符串
{
while(j < i)
str[index++] = str[j++];
}
}
i++;
}
for(i = 0; i < index; i++)
printf("%c", str[i]);
printf(",%d\n", max);
}
}