在一个字符串中找出最长的数字串,并把长度返回,如果存在长度相同的连续数字串,返回最后一个连续数字串。
#include <iostream>
#include <string>
using namespace std;
int test(){
string str;
cin >> str;
string max_str;
int max = 0;
int i = 0;
while (i < str.size()){
string temp;
int cnt=0;
while (isdigit(str[i])){
temp += str[i++];
cnt++;
}
// 相同也要返回最后一个
if (cnt >= max){
max = cnt;
max_str = temp;
}
i++;
}
cout << max_str << endl;
cout << max << endl;
return EXIT_SUCCESS;
}
结果:
abcd12345ed125ss123058789
123058789
9
请按任意键继续. . .