字符串怎么运用
小T原本在一个电脑文档中记录了他新申请的QQ号,但因为贪玩胡乱敲打键盘导致原本的数字串里多了很多 英文字母和其它符号(已知没有碰到空格、回车、和数字区的键),请你帮小T将这串混乱的字符串恢复成原本 只含数字的QQ号
遍历字符串,过滤非数字字符就行了吧
#include <iostream>
using namespace std;
int main()
{
char s[1000];
gets(s);
int i=0,j=0;
while(s[i] != '\0')
{
if(s[i] >= '0' && s[i] <='9')
s[j++] = s[i];
i++;
}
s[j] = '\0';
cout<<s<<endl;
return 0;
}
供参考:
#include <stdio.h>
#include <ctype.h>
int main()
{
char str[256];
int i = 0, d = 0;
gets(str);
while (str[i])
{
if (isdigit(str[i]))
str[d++] = str[i];
i++;
}
str[d] = '\0';
printf("QQ:%s", str);
return 0;
}
#include <iostream>
#include <cstring>
using namespace std;
int main()
{
string str; cin >> str;
string res = "";
int len = str.length();
for (int i = 0; i < len; i++)
{
if (str[i] >= '0' && str[i] <= '9')
{
res += str[i];
}
}
cout << res << endl;
return 0;
}
#include<iostream>
using namespace std;
int main()
{
string s1;
cin >> s1;
for (int i = 0;i < s1.size();i++)
{
if (s1[i] >= '0' && s1[i] <= '9')
{
cout << s1[i];
}
}
}