读写键盘输入直到遇到@,并回显输入,数字除外。同时将大写字母转化为小写,小写字母转化为大写。利用cctype函数。
#include<iostream> #include<cctype> #include<string> using namespace std; void main() { int n,i; cout<<"输入文本,以@结束测试.\n"; char ch; int digits=0;//数字 int others=0; string str; cin.get(ch); while(ch!='@') { if(islower(ch)) { ch=toupper(ch); str=str+ch; } else if(isupper(ch)) { ch=tolower(ch); str=str+ch; } else if(isdigit(ch)) digits++; else str=str+ch; cin.get(ch); } cout<<str<<endl; cout<<"还有"<<digits<<"个数字没有显示出来"<<endl; }
笔记:char可以直接拼接到string后面。
浙公网安备 33010602011771号