您的位置:首页 > 其它

华为机试——字符串字母转换(二)

2015-11-26 16:03 288 查看

题目:输入一个字符串,将其中大写字母转换为对应小写字母之后的第五个字母,若原始大写字母为V~Z,则转换为对应小写字母的值减21。其他字符变换,输出转换后的字符串。例如,对于字母A,则转换为小写字符f;如形参是字母W,则转换为小写字母b。

样例输入:Axs3mWss

样例输出:fxs3mbss

#include<iostream>
#include<string>
#include<cctype>
using namespace std;

string convert(string s)
{
string::size_type i=0;
string result;
char ch;
for(;i<s.size();i++)
{
if(isupper(s[i]))
{
if(s[i]>='V' && s[i]<='Z')
{
ch=tolower(s[i])+5-26;
result.push_back(ch);
}
else
{
ch=tolower(s[i])+5;
result.push_back(ch);
}
}
else
result.push_back(s[i]);
}
return result;
}

int main()
{
string str;
getline(cin,str);
cout<<convert(str)<<endl;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: