Changing a capital letter (read from cin) to lower case?

前端 未结 5 1990
独厮守ぢ
独厮守ぢ 2021-01-29 08:13

i wanna change the capital cin to lower case for the input , for example if cin>> one one=R it should be r so it convert it automatically

<
相关标签:
5条回答
  • 2021-01-29 08:13

    You can write your code using transform:

    string one;
    string two;
    cout << "\nPlayer One, please enter your move: ('p'  for Paper, 'r' for Rock, '" << std::endl;
    cin >> one;
    
    std::transform(one.begin(), one.end(), one.begin(), ::tolower);
    cout <<"\nPlayer Two, please enter your move: ('P' for Paper, 'R' for Rock, 'S'" << std::endl;
    cin >> two;
    std::transform(two.begin(), two.end(), two.begin(), ::tolower);
    std::cout << "one=" << one << " ; two=" << two << std::endl;
    

    The output may be as follows (for R, P):

    Player One, please enter your move: ('p'  for Paper, 'r' for Rock, '
    R
    
    Player Two, please enter your move: ('P' for Paper, 'R' for Rock, 'S'
    P
    one=r ; two=p
    
    0 讨论(0)
  • 2021-01-29 08:19

    the transform is the right way to do it, i think, you just need to include the algorithm library

    #include <ctype>
    #include <algorithm>
    #include <string>
    
    std::transform(one.begin(), one.end(), one.begin(), ::tolower); 
    std::transform(two.begin(), two.end(), two.begin(), ::tolower);
    
    0 讨论(0)
  • 2021-01-29 08:23

    You should store your character in variable and change the variable

    #include <cctype>
    #include <iostream>
    
    using namespace std;
    int main ()
    {
        char one{};
        char two{};
    cout << "\nPlayer One, please enter your move: ('p'  for Paper, 'r' for Rock, '$
    cin >> one;
    one = tolower(one);
    
    cout <<"\nPlayer Two, please enter your move: ('P' for Paper, 'R' for Rock, 'S'$
    cin >> two;
    one = tolower(two);
    
    0 讨论(0)
  • 2021-01-29 08:32

    In header file " ctype.h" there are functions which supports case formatting

    such as : tolower(var) formats text to lower case, toupper(var) formats text to upper case

    0 讨论(0)
  • 2021-01-29 08:40

    Try:

    #include <ctype>
    
    tolower(char)
    

    For a string input, you'll have to convert character by character:

    char str[]="ONE TWO\n";
    char c;
    int i=0;
    while (str[i])
    {
      c=str[i];
      str[i] = (char) tolower(c);  //I think it return an int, which you need to typecast to a char
      i++;
    }
    
    0 讨论(0)
提交回复
热议问题