【分享】笔试面试经典之字符串中的数字输出(strtol)

funywork 2012-03-07 04:02:20
字符串中的数字输出可谓经典,半年多的时间里在笔试面试中出现了十几次(我在找工作),面试官们也比较喜欢拿字符串的问题作文章。今天看了一个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;
}

学习中,欢迎交流!
...全文
53 3 打赏 收藏 转发到动态 举报
写回复
用AI写文章
3 条回复
切换为时间正序
请发表友善的回复…
发表回复
qinjun312 2012-03-07
  • 打赏
  • 举报
回复
没有考虑大数问题 当超过LONG 长度后 输出不正确
funywork 2012-03-07
  • 打赏
  • 举报
回复
潜水专业户,很少发帖,这个尴尬了。。。
http://blog.csdn.net/funywork/article/details/7328380

64,654

社区成员

发帖
与我相关
我的任务
社区描述
C++ 语言相关问题讨论,技术干货分享,前沿动态等
c++ 技术论坛(原bbs)
社区管理员
  • C++ 语言社区
  • encoderlee
  • paschen
加入社区
  • 近7日
  • 近30日
  • 至今
社区公告
  1. 请不要发布与C++技术无关的贴子
  2. 请不要发布与技术无关的招聘、广告的帖子
  3. 请尽可能的描述清楚你的问题,如果涉及到代码请尽可能的格式化一下

试试用AI创作助手写篇文章吧