17. 字符串数字置换

【问题描述】

从键盘接收用户输入的字符串, 对用户输入的每个字符串的处理是:将字符串内的每一个十进制数字字符置换成下列表格中右边所对应的一个字符串(所有其他字符不变),然后将转换的结果显示在屏幕上;并分别计算每个数字的置换次数。

十进制数字字符

置换成

0

(Zero)

1

(One)

2

(Two)

3

(Three)

4

(Four)

5

(Five)

6

(Six)

7

(Seven)

8

(Eight)

9

(Nine)

例如,若用户输入的字符串为

         Page112-Line3,

则程序5的输出是:

         Page(One) (One) (Two)-Line(Three),

数字0到9的置换次数分别是  0 2 1 1 0 0 0 0 0 0

【输入形式】

输入一行字符串,其中可包含字母、数字、空格或其他符号(英文)

【输出形式】

第一行为将字符串中的数字转换为表格中的内容后输出

第二行为数字0~9被转换的次数

【样例输入】

Page112-Line3

【样例输出】

Page(One)(One)(Two)-Line(Three)
0 2 1 1 0 0 0 0 0 0
#include<bits/stdc++.h>
using namespace std;
string change(char);//将数字转换为对应字符
int main() {
	map<int,int>num;
	for(int i=0; i<10; i++) {
		num[i]=0;
	}
	string str1;
	getline(cin,str1);
	char tempc;
	for(int i=0; i<str1.size(); i++) {
		tempc=str1[i];
		if(tempc>='0'&&tempc<='9') { //满足置换条件
			cout<<change(tempc);
			num[tempc-'0']++;
		} else {
			cout<<tempc;
		}
	}
	cout<<endl;
//	map<int,int>::iterator it;
//	for(it=num.begin(); it!=num.end(); it++) {
//		cout<<it->second<<' ';
//	}
	for(int i=0; i<10;i++)
	cout<<num[i]<<' ';
		return 0;
}
string change(char a) {
	string temp;
	switch(a) {
		case '0': {
			temp="(Zero)";
			break;
		}
		case '1':
			temp="(One)";
			break;
		case '2':
			temp="(Two)";
			break;
		case '3':
			temp="(Three)";
			break;
		case '4':
			temp="(Four)";
			break;
		case '5':
			temp="(Five)";
			break;
		case '6':
			temp="(Six)";
			break;
		case '7':
			temp="(Seven)";
			break;
		case '8':
			temp="(Eight)";
			break;
		case '9':
			temp="(Nine)";
			break;
	}
	return temp;
}

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值