只要找到最长数字串的起始位置,还有长度,就OK了
#include<iostream>
#include<string>
using namespace std;
void main(){
string s;
cin >> s;
int len = s.size(), j = 0,l=0,max=0,k=0;
for (int i = 0; i < len; i++){
if (s[i] >= '0'&&s[i] <= '9'){
if (l == 0)
j = i; //j标记数字起始位置
l++;
if (l>max){
max = l;
k = j; //K为最长字符串的起始位置。
}
}
else
l = 0; //若不是数字,则将长度置为0
}
string temp = s.substr(k, max);//从k起,长度为max的字符。
if (max == 0)
cout << max << endl;
else
cout << temp << "," <<max<< endl;
system("pause");
}