#include
#include
using namespace std;
int main()
{
string str;
cin >> str;
int nPos = -1;
int nCount = 0;
string strTmp;
for (int i = 0; i < str.size(); i++)
{
if (str[i] >= '0' && str[i] <= '9') // 判断是不是数字
{
nPos = i;
for (; i < str.size(); i++)
{
if (str[i] <= '0' || str[i] >= '9') // 不是数字就停下
{
i--;
break;
}
}
strTmp = str.substr(nPos, i - nPos + 1);
cout << "Digit string " << ++nCount << " " << "is " << strTmp << endl;
}
}
return 0;
}
朋友,请【采纳答案】,您的采纳是我答题的动力,如果没有明白,请追问。谢谢。
int main() {
string str = "A012BCD378a 274D55asdf";
int count = 0;
string digit;
for (unsigned int i = 0; i < str.size(); ++i){
if (isdigit(str[i])) {
++count;
for ( ; i < str.size(); ++i) {
if (isdigit(str[i])) digit += str[i];
else break;
}
cout << "Digit string " << count << " is: " << digit << endl;
digit.clear();
}
}
return 0;
}