字符串中的数字输出可谓经典,半年多的时间里在笔试面试中出现了十几次(我在找工作),面试官们也比较喜欢拿字符串的问题作文章。今天看了一个strtol函数,觉得用来解决这个问题非常好。
#include <iostream>
using namespace std;
int main()
{
char string[100] = {0},*stopString = NULL;
int x;
cout<<"Input: ";
cin>>string;
cout<<"The numbers are: "<<endl;
char *temp = string;
while( *temp )
{
x = strtol( temp,&stopString,10);
if(temp == stopString)
{
temp++;
}
else
{
temp = stopString;
cout<<x<<" ";
}
}
return 0;
}
学习中,欢迎交流!