【问题标题】:Pig Latin converter using toupper使用 toupper 的 Pig Latin 转换器
【发布时间】: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


【解决方案1】:

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

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

toupper(first[0]);

“看不到明显缺失的东西”综合症的常见情况......我讨厌那些错误。

至于将第一个字符移到末尾,我通常只使用 substr() 来表示简单的情况:

str = str.substr(1) + str[0];

【讨论】:

  • 非常感谢!
猜你喜欢
  • 2021-12-13
  • 1970-01-01
  • 2016-05-02
  • 2016-01-13
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多