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

前端 未结 5 1995
独厮守ぢ
独厮守ぢ 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条回答
  •  慢半拍i
    慢半拍i (楼主)
    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
    

提交回复
热议问题