猪拉丁转换器使用toupper

问题描述 投票:0回答:1

我在使用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;

}
c++ string loops character
1个回答
2
投票

只是一个简单的错误,比较

first[x] = tolower(first[x]);

toupper(first[0]);

通常情况下'看不到明显的遗漏'综合症......我讨厌那些错误。

至于将第一个字符移动到最后我通常只使用substr()作为一个简单的例子:

str = str.substr(1) + str[0];
© www.soinside.com 2019 - 2024. All rights reserved.