C++ 怎样从字符串中获取数字,并存入数组中?

Wind_1987 2008-10-07 10:31:16
C++ 怎样从字符串中获取数字,并存入数组中?
...全文
740 6 打赏 收藏 转发到动态 举报
写回复
用AI写文章
6 条回复
切换为时间正序
请发表友善的回复…
发表回复
zhaokai4711280 2008-10-07
  • 打赏
  • 举报
回复
一个一个的找
kkndciapp 2008-10-07
  • 打赏
  • 举报
回复
先找到数字,然后存入
星羽 2008-10-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <vector>
using namespace std;

int main() {

char str[] = "uu123aaa3435ddd9989a1";
char tmp[32];
size_t i = 0;
vector<int> vInts;

char* p = str;

while (true) {
if (isdigit(*p)) {
tmp[i++] = (*p);
}
else {
if (i) {
tmp[i] = 0;
vInts.push_back(atoi(tmp));
}
i = 0;
}
if (!(*p)) {
break;
}
++p;
}

for (i = 0; i < vInts.size(); ++i)
cout<<vInts[i]<<endl;

return 0;
}
zclever 2008-10-07
  • 打赏
  • 举报
回复
1楼的输出有问题

//cout<<newstring<<endl;
for(i=0;i<j;i++)
cout<<newstring[i];
thecrypig 2008-10-07
  • 打赏
  • 举报
回复
字符串是字符的串 属char
数字是int

是不是如 “jjojoijo888johh” 中888啊?

让它一个个输出ascii值 在48~57间的就是数字了
再转换成数字就ok了!

zclever 2008-10-07
  • 打赏
  • 举报
回复

#include <iostream>
#include <string>
using namespace std;
int main()
{
char *p="hello123world";
int i,j;
int len=strlen(p);
char *newstring =new char[len+1];
memset(newstring,0,sizeof(newstring));
for(i=0,j=0;i<len;i++)
{
if(isdigit(p[i]))
newstring[j++]=p[i];
}
cout<<newstring<<endl;
return 0;
}

64,637

社区成员

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

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