#include <iostream> #include <cctype> using namespace std; int main() { string str; cin>>str; int a[27]={2,2,2,3,3,3,4,4,4,5,5,5,6,6,6,7,7,7,7,8,8,8,9,9,9,9}; for(int i=0;i<str.length();i++) { if(isupper(str[i])) { int x=str[i]+33; if(x>122) cout<<'a'; else cout<<char(x); } else if(islower(str[i])) { int k=str[i]-97; cout<<a[k]; } else cout<<str[i]; } return 0; }
由题三个条件,可分别判断各个情况,isupper()可用来判断是不是大写字母,islower()用来判断是否是小写字母,两个函数都在<cctype>头文件里,也可以直接用ascii码判断。
由题可知大写字母转换为对应小写字母的后一位,即使ascii码加上33,若原字母为Z,则特判转换为a。
小写字母直接用一个数组来存对应的9键数字
数字保持不变