C++实现查找字符串中的数字,并输出

例如输入:dsafjoi3425sfsdjl5435asfkl--=3400
输出为:3425 5434 3400 


#include<iostream>
#include<string>
using namespace std;
void findnum(string & ch);
bool isnum(char n);
int main()
{
string m ;
cout << "Enter a string:\n";

while (cin>>m&&cin.get() != '\n');
 
findnum(m);

return 0;
}

void findnum(string & ch)
{
int k = ch.size();
int  * num = new int[k];
int result;
int n = 0;
int i = 0;
while (n<k) {
result = 0;
if (isnum(ch[n]))
{
result = ch[n] - '0';
while (n<k&&isnum(ch[++n]))
result = (ch[n] - '0') + 10 * result;
num[i++] = result;
}
++n;
}
for (int j = 0; j <i; ++j)
cout << num[j] << "\t";;
}

bool isnum(char n)
{
return (n >= '0'&&n <= '9');
}

猜你喜欢

转载自blog.csdn.net/walker_m/article/details/80148073