调用函数打印a-z。小写到大写

时间:2019-06-11 04:41:47

标签: c++

我有一个接受小写字母并返回大写字母的函数。 我还有一个函数,该函数接受大写字母并返回小写字母。

在转换您选择的第一个字母后,我想调用执行此转换的函数以全部大写或小写形式打印字母。

例如函数将“ a”变成“ A” 完成此操作后: 现在,以大写字母打印A-Z。

答案要求。

https://justpaste.it/7f95f

下面提供的代码。

#include <iostream>

using namespace std;

char ToUpperCase(char _cInput);
char ToLowerCase(char _cInput);
char Letter;
int Menu;

int main() {
  cout << "This program changes any letter from lowercase to uppercase, it "
          "leaves uppercase letters as they already are."
       << endl;
  cout << endl;

  cout << "Menu: " << endl;
  cout << endl;
  cout << "1) Convert a lowercase letter to uppercase." << endl;
  cout << endl;
  cout << "2) Convert an uppercase letter to a lowercase one." << endl;
  cout << endl;
  cout << "3) Quit." << endl;
  cout << endl;

  cin >> Menu;

  while (Menu > 3 || cin.fail()) {
    cout << "Use only the options avaliable." << endl;
    cout << endl;
    cin.clear();  // clear input buffer to restore cin to a usable state
    cin.ignore(); // ignore last input
    cin >> Menu;
  }

  if (Menu == 1) {
    cout << "Taking you to the Lowercase to Uppercase converter." << endl;
    cout << endl;

    cout << "Input a lowercase letter." << endl;
    cin >> Letter;

    while (isdigit(Letter)) {
      cout << Letter << ": Is not an acceptable input. Try again." << endl;
      cin >> Letter;
    }

    cout << ToUpperCase(Letter) << endl;
  } else if (Menu == 2) {
    cout << "Taking you to the Uppercase to Lowercase converter." << endl;
    cout << endl;

    cout << "Input an uppercase letter." << endl;
    cin >> Letter;

    while (isdigit(Letter)) {
      cout << Letter << ": Is not an acceptable input. Try again." << endl;
      cin >> Letter;
    }

    cout << ToLowerCase(Letter) << endl;
  } else if (Menu == 3) {
    cout << "Quitting Now." << endl;
    cout << endl;
    return (0);
  }

  int iTemp;
  cin >> iTemp;
}

char ToUpperCase(char _cInput) {
  if (Letter >= 'A' && Letter <= 'Z') {
    cout << "This is already an Uppercase letter." << endl;
    cout << endl;
  } else if (Letter >= 'a' && Letter <= 'z') {
    _cInput = Letter - 32;

    cout << "This is a Lowercase letter." << endl;
    cout << endl;

    cout << "The Uppercase version of this letter is: ";
  }

  return (_cInput);
}

char ToLowerCase(char _cInput) {
  if (Letter >= 'A' && Letter <= 'Z') {
    cout << "This is an Uppercase letter." << endl;
    cout << endl;

    _cInput = Letter + 32;

    cout << "The Lowercase version of this letter is: ";
  } else if (Letter >= 'a' && Letter <= 'z') {
    cout << "This is already a Lowercase letter." << endl;
    cout << endl;
  }
  return (_cInput);
}

https://pastecode.xyz/view/1a18904b

2 个答案:

答案 0 :(得分:0)

如何通过调用“ ToUpperCase”功能以大写字母打印a-z? 根据您的问题,您想打印字符串,波纹管可能是打印的一种方式。

#include<iostream> 
using namespace std; 

void toUpperCase(string &str) 
{ 
    int ln = str.length(); 
    for (int i=0; i<ln; i++) 
    { 
        if (str[i]>='a' && str[i]<='z') 
        {
            str[i] = str[i] - 32; 
        }
    } 
} 

int main() 
{ 
    string str = ""; 
    cout<< "Enter the string ";
    cin >> str;

    toUpperCase(str); 

    cout << str; 
    return 0; 
} 

如果要从上到下,则只需

if(str[i]>='A' && str[i]<='Z') 
str[i] = str[i] + 32;

答案 1 :(得分:0)

您可以使用uc_mcontext.error_code来运行将函数应用于数组所有元素的循环。

std::transform