题目:
第四次实验:字符串
从键盘输入一个字符串s,形如:
"abc345def"
里面有一个数字字符子串(如上面串中的“345”),将其取出,并且在其数值上 + 100,输出运算结果。
题解:
#include <iostream>
#include <stdlib.h>
const int N = 1000;
using namespace std;
//第四次实验:字符串
//从键盘输入一个字符串s,形如:
//"abc345def"
//里面有一个数字字符子串(如上面串中的“345”),将其取出,并且在其数值上 + 100,输出运算结果。
int main()
{
string s;
int i = 0;
char res[N] = "";//保存数字字符子串,并初始化
cin >> s;
int len = s.length();//获取字符串长度
for (char c : s)
if (c >= '0' && c <= '9')//如果是一个数字
res[i++] = c;//就保存在res中
int num = atoi(res) + 100 ;//从字符串到数字转化
cout << num;
}