更改大写字母(从cin读取)到小写? [英] Changing a capital letter (read from cin) to lower case?

查看:207
本文介绍了更改大写字母(从cin读取)到小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


我想改变大写字母为小写的输入,例如如果cin >> one
one = R它应该是r所以它自动转换




  #include< cctype> 
#include< iostream>

using namespace std;
int main()
{
string one;
string two;
cout<< \ nPlayer One,请输入您的移动:('p'表示纸张,'r'表示岩石,'$
cin>>一个;

cout< \ nPlayer Two,请输入您的移动:('P'表示纸张,'R'表示岩石,'S'表示$
cin表示两个;


解决方案

您可以使用transform编写代码:



<$一个,请输入您的移动:('p'表示纸张,'r'表示纸张,对于Rock,'< cin>> one;

std :: transform(one.begin(),one.end one.begin(),:: tolower);
cout< <\\\
Player二,请输入您的移动:('P'表示纸,'R'表示摇滚,'S'& < std :: endl;
cin>> two;
std :: transform(two.begin(),two.end(),two.begin(),:: tolower);
std :: cout<<one =<

输出可能如下(对于R,P):

 玩家一,请输入您的行动:('p'表示纸,'r'表示摇滚,'
R

玩家二,请输入你的移动:('P'为纸,'R'为摇滚,'S'
P
one = r; two = p


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

    #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;

解决方案

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

这篇关于更改大写字母(从cin读取)到小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆