[英]Character converting funtion std::isupper() & std::islower() C++17
I created a simple program to check whether the letter that a user has inputed is either uppercase or lowercase and then convert the lowercase to uppercase and the uppercase to lowercase using the std::isupper()
and std::islower()
funtion.我创建了一个简单的程序来检查用户输入的字母是大写还是小写,然后使用
std::isupper()
和std::islower()
函数将小写转换为大写,将大写转换为小写。 upon running the code I get the character converion in number form instead of the expected uppercase/lowercase equivalent.在运行代码时,我得到数字形式的字符转换,而不是预期的大写/小写等效。 Why is that?
这是为什么?
#include <iostream>
int main()
{
char letter {};
std::cout << "Enter a letter:";
std::cin >> letter;
if (std::isupper(letter))
{
std::cout << "You entered an uppercase letter"
"\n"
"the lowercase equivalent is:"
<< std::tolower(letter);
}
if (std::islower(letter))
{
std::cout << "You entered a lowercase letter"
"\n"
"the uppercase equivalent is:"
<< std::toupper(letter);
}
return 0;
}
Here is an example of an output below:下面是一个输出示例:
Enter a letter:F
You entered an uppercase letter.
The lowercase equivalent is:102
Enter a letter:f
You entered a lowercase letter.
The uppercase equivalent is:70
std::tolower
and std::toupper
return int
, not char
( due to it's legacy origin from C there are certain requirements due to which int
was chosen, see footnote). std::tolower
和std::toupper
返回int
,而不是char
(由于它是 C 的遗留起源,因此选择了int
有某些要求,请参见脚注)。
You can cast it back to char to get expected results:您可以将其转换回 char 以获得预期结果:
static_cast<char>(std::tolower(letter));
Or you could save the result in a char
variable before (if you need that converted latter elsewhere):或者您可以之前将结果保存在
char
变量中(如果您需要在其他地方转换后者):
letter = std::tolower(letter);
std::cout << letter;
Note: As noticed by Peter in comment, there are requirements for std::tolower
and std::toupper
that mandate use of type bigger than type actually needed.注意:正如Peter在评论中所注意到的,对
std::tolower
和std::toupper
有要求,要求使用比实际需要的类型大的类型。 Quoting it below:在下面引用它:
They are also specified as being able to accept and return
EOF
- a value that cannot be represented as a char but can be represented as an int.它们还被指定为能够接受和返回
EOF
- 一个不能表示为 char 但可以表示为 int 的值。 C++ iostreams (certainly no legacy from C, being specializations of the templatedstd::basic_istream
) have aget()
function with no arguments that reads a character from the stream, and returns an integral type larger than the character type being read.C++ iostreams(当然不是来自 C 的遗产,是模板化
std::basic_istream
)有一个没有参数的get()
函数,它从流中读取一个字符,并返回一个大于正在读取的字符类型的整数类型。 It is part of the machinery of being able to read a single character from a file and deal with error conditions.它是能够从文件中读取单个字符并处理错误情况的机制的一部分。
You can use std::tolower
and std::toupper
from <locale>
header that return the type you would expect them to return.您可以使用
<locale>
标头中的std::tolower
和std::toupper
返回您希望它们返回的类型。
Take a look at the examples:看一下例子:
char c {'T'};
std::cout << std::tolower(c, std::locale()) << std::endl; // output: t
and和
char c {'t'};
std::cout << std::toupper(c, std::locale()) << std::endl; // output: T
Check live example检查实时示例
You can use std::tolower
and std::toupper
from <cctype>
header that return int
that you need to cast to char
.您可以使用
std::tolower
和std::toupper
从<cctype>
标头,返回int
,你需要强制转换为char
。
Here are the examples:以下是示例:
char c {'T'};
std::cout << static_cast<char>(std::tolower(c)) << std::endl; // output: t
and和
char c {'t'};
std::cout << static_cast<char>(std::toupper(c)) << std::endl; // output: T
Check live example检查实时示例
You can also create your own handy helper functions:您还可以创建自己的方便的辅助函数:
char toupper(char c) {
return static_cast<char>(std::toupper(static_cast<unsigned char>(c)));
}
char tolower(char c) {
return static_cast<char>(std::tolower(static_cast<unsigned char>(c)));
}
which you can use like this:你可以这样使用:
char c1 {'T'};
char c2 {'t'};
std::cout << tolower(c1) << std::endl; // output: t
std::cout << toupper(c2) << std::endl; // output: T
if(std::isupper(letter))
{
std::cout<<"You entered an uppercase letter"<<"\n"
"the lowercase equivalent is:" << (char)std::tolower(letter);
}
if (std::islower(letter))
{
std::cout<<"You entered a lowercase letter"<<"\n"
"the uppercase equivalent is:" << (char)std::toupper(letter);
}
return 0;
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.