从大写C ++变为小写

时间:2014-03-05 06:18:33

标签: c++

我想将输入从大写更改为小写,例如。如果输入是R - 它变为r。我希望它与字符串而不是char。我试过这个

one=tolower(one);
two=tolower(two);

但它不起作用。

#include <cctype>
#include <iostream>
using namespace std;

int main () {
    string one;
    string two;
    cout << "\nPlayer One, please enter your move: ('p'  for Paper, 'r' for Rock, '$
    cin >> one;

    cout <<"\nPlayer Two, please enter your move: ('P' for Paper, 'R' for Rock, 'S'$
    cin >> two;

3 个答案:

答案 0 :(得分:2)

您可以使用std::transform和右std::tolower重载:

#include <cctype>    // std::tolower
#include <algorithm> // std::transform

std::transform(one.begin(), one.end(), one.begin(),
               [](char c)
               { return std::tolower(static_cast<unsigned char>(c));});

答案 1 :(得分:0)

您可以执行以下操作:

for(i = 0;i < one.size();i++)
{
    if(one[i]>='A' && one[i]<='Z')
    {
        one[i] = one[i]-'A'+'a';
    }
}

答案 2 :(得分:0)

试试这个:

#include <algorithm>
#include <string> 
std::transform(one.begin(), one.end(), one.begin(), ::tolower);
相关问题