C++语言_从一字符串中读取数字

要求任意输入一字符串,如:dprf4.0jf2m3.2
最后输出:4.0,2,3.2
谢谢!
最后输出是分开的,4.0 2 3.2
谢谢!

第1个回答  2010-04-27
#include<iostream>
using namespace std;

void main()
{
char c;
char a[20];
cout<<"输入(20位):"<<endl;
for(int i=0; i<20; i++)
{ cin>>c; a[i]=c; }
for(i=0; i<20; i++)
if(a[i]>='0' && a[i]<='9')
cout<<a[i];
cout<<endl;
}
第2个回答  2010-04-27
对字符串的每个char型字符判断是否介于48到57之间,48是0的ASCII码,57是9的ASCII码,成立的话输出。
第3个回答  2010-04-30
lz我鄙视你.
这是赵作业抄吧,','到' '都不会改啊
哥这分不要也罢,代码删掉了
第4个回答  2010-04-27
给个思路
char s[1028];
scanf("%s",s);
int len=strlen(s);
for(int i=0;i<len;i++)
{
if(s[0]>='0' && s[0]<='9')
printf("%c ",s[i]);
}本回答被网友采纳
相似回答