【发布时间】:2016-04-29 04:28:29
【问题描述】:
在字符串的第一个字符上使用 toupper 转换时遇到问题。
我使用tolower(first[0])将第一个字母变成小写。
toupper(first[0]) 为什么不把第一个字符大写?
另外,有没有办法将字符串中的第一个字符移动到最后一个位置?
非常感谢。
#include <iostream>
#include <string>
using namespace std;
int main ()
{
char ans;
do{
string first, last;
char first_letter, first_letter2;
cout << "This program will convert your name "
<< "into pig latin.\n";
cout << "Enter your first name: \n";
cin >> first;
cout << "Enter your last name: \n";
cin >> last;
cout << "Your full name in pig latin is ";
for(int x = 0; x < first.length(); x++){
first[x] = tolower(first[x]);
}
for(int x = 0; x < last.length(); x++){
last[x] = tolower(last[x]);
}
first_letter = first[0];
bool identify;
switch (first_letter)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify = true;
break;
default:
identify = false;
}
if(identify == true){
toupper(first[0]);
cout << first << "way" << " ";
}
first_letter2 = last[0];
bool identify2;
switch (first_letter2)
{
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
identify2 = true;
break;
default:
identify2 = false;
}
if(identify2 == true){
toupper(first[0]);
cout << last << "way" << endl;
}
cout << "You you like to try again? (Y/N)\n";
cin >> ans;
} while(ans == 'y' || ans == 'Y');
return 0;
}
【问题讨论】:
-
我需要先将它们全部小写,以防用户输入所有大写字母。程序的最终输出应该是猪拉丁语中的姓名和姓氏,只有第一个字母大写(这就是为什么我需要在 switch 语句之后使用 toupper),例如 Oscarway Allenway。
标签: c++ string loops character