简体   繁体   English

字符转换函数 std::isupper() & std::islower() C++17

[英]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::tolowerstd::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::tolowerstd::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 templated std::basic_istream ) have a get() 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.它是能够从文件中读取单个字符并处理错误情况的机制的一部分。

1. option 1. 选项

You can use std::tolower and std::toupper from <locale> header that return the type you would expect them to return.您可以使用<locale>标头中的std::tolowerstd::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检查实时示例

2. option 2. 选项

You can use std::tolower and std::toupper from <cctype> header that return int that you need to cast to char .您可以使用std::tolowerstd::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.

相关问题 std :: launder alternative pre c ++ 17 - std::launder alternative pre c++17 std :: addressof作为C ++中的常量表达式17 - std::addressof as a constant expression in C++17 C ++ 17标准是否包含“std :: byte”? - Will the C++17 standard include “std::byte”? 在c ++ 17中使用std :: destroy_at()? - Usage of std::destroy_at() in c++17? std :: map ::在C ++中插入更改17 - std::map::insert change in C++17 std::tie 用于 C++17 结构运算符<!--?</div--><div id="text_translate"><p> 考虑以下 C++17 结构:</p><pre> struct S { M1 m1; M2 m2; M3 m3; bool operator<(const S& that) const { return tie() < that.tie(); } auto tie() const { return std::tie(m1,m2,m3); } };</pre><p> 这个对吗? S::tie会返回成员引用的元组,还是会复制一份? 会自动推断出正确的类型(引用元组)吗? constness 做正确的事吗?</p><p> (我看到的示例对 std::tie 进行了两次调用,并且没有像这样分解为单独的成员 function。想知道/怀疑是否有原因。)</p></div> - std::tie for C++17 struct operator<? 为什么在C ++ 17中没有std :: future :: then? - Why is there no std::future::then in C++17? std :: function with C ++中的noexcept 17 - std::function with noexcept in C++17 C ++ 17 std :: G ++中的可选项? - C++17 std::optional in G++? 在C ++ 17中,“std :: is_callable”是否替换为“std :: is_invocable”? - Is “std::is_callable” replaced with “std::is_invocable” in C++17?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM