在 C++ 中将单个字符转换为小写 - tolower 返回一个整数

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33849637/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-28 14:19:19  来源:igfitidea点击:

Convert a single character to lowercase in C++ - tolower is returning an integer

c++stringcharlowercasetolower

提问by user83676

I'm trying to convert a string to lowercase, and am treating it as a char* and iterating through each index. The problem is that the tolowerfunction I read about online is not actually converting a char to lowercase: it's taking char as input and returning an integer.

我正在尝试将字符串转换为小写,并将其视为 char* 并遍历每个索引。问题是tolower我在网上读到的函数实际上并没有将 char 转换为小写:它以 char 作为输入并返回一个整数。

cout << tolower('T') << endl;

prints 116to the console when it should be printing T.

116在应该打印时打印到控制台T

Is there a better way for me to convert a string to lowercase? I've looked around online, and most sources say to "use tolowerand iterate through the char array", which doesn't seem to be working for me.

有没有更好的方法将字符串转换为小写?我在网上环顾四周,大多数消息来源都说“使用tolower并遍历字符数组”,这似乎对我不起作用。

So my two questions are:

所以我的两个问题是:

  1. What am I doing wrong with the tolowerfunction that's making it return 116 instead of 't' when I call tolower('T')

  2. Are there better ways to convert a string to lowercase in C++ other than using toloweron each individual character?

  1. 我在tolower调用时返回 116 而不是 't'的函数有什么问题tolower('T')

  2. 除了tolower在每个单独的字符上使用之外,是否有更好的方法在 C++ 中将字符串转换为小写?

回答by Barry

That's because there are two different tolowerfunctions. The one that you're using is this one, which returns an int. That's why it's printing 116. That's the ASCII value of 't'. If you want to print a char, you can just cast it back to a char.

那是因为有两个不同的tolower功能。您正在使用的是this one,它返回一个int. 这就是它打印 116 的原因。这是't'. 如果你想打印 a char,你可以将它转换回 a char

Alternatively, you could use this one, which actually returns the type you would expect it to return:

或者,您可以使用this one,它实际上返回您希望它返回的类型:

std::cout << std::tolower('T', std::locale()); // prints t

In response to your second question:

针对你的第二个问题:

Are there better ways to convert a string to lowercase in C++ other than using tolower on each individual character?

除了在每个单独的字符上使用 tolower 之外,是否有更好的方法在 C++ 中将字符串转换为小写?

Nope.

不。

回答by user4578093

116 is indeed the correct value, however this is simply an issue of how std::couthandles integers, use char(tolower(c))to achieve your desired results

116 确实是正确的值,但这只是如何std::cout处理整数的问题,用于char(tolower(c))实现您想要的结果

std::cout << char(tolower('T')); // print it like this

回答by Alan Stokes

It's even weirder than that - it takes an intand returns an int. See http://en.cppreference.com/w/cpp/string/byte/tolower.

它甚至比这更奇怪 - 它需要一个int并返回一个int. 请参阅http://en.cppreference.com/w/cpp/string/byte/tolower

You need to ensure the value you pass it is representable as an unsigned char- no negative values allowed, even if charis signed.

您需要确保您传递的值可以表示为unsigned char- 不允许负值,即使char是有符号的。

So you might end up with something like this:

所以你可能会得到这样的结果:

char c = static_cast<char>(tolower(static_cast<unsigned char>('T')));

Ugly isn't it? But in any case converting one character at a time is very limiting. Try converting '?' to upper case, for example.

丑是不是?但无论如何,一次转换一个字符是非常有限的。尝试转换 '?' 例如,大写。

回答by Richard Hodges

the documentation of int to_lower(int ch)mandates that ch must either be representable as an unsigned charor must be equal to EOF(which is usually-1, but don't rely on that).

int to_lower(int ch)ch 必须可以表示为 anunsigned char或必须等于EOF通常为-1,但不要依赖于它)的指令文档。

It's not uncommon for character manipulation functions that have been inherited from the c standard library to work in terms of ints. There are two reasons for this:

从 c 标准库继承的字符操作函数以整数形式工作的情况并不少见。有两个原因:

  1. In the early days of C, all arguments were promoted to int (function prototypes did not exist).

  2. For consistency these functions need to handle the EOF case, which for obvious reasons cannot be a value representable by a char, since that would mean we'd have to lose one of the legitimate encodings for a character.

  1. 在 C 的早期,所有参数都被提升为 int(函数原型不存在)。

  2. 为了保持一致性,这些函数需要处理 EOF 情况,由于显而易见的原因,这不能是 a 表示的值char,因为这意味着我们必须丢失字符的合法编码之一。

http://en.cppreference.com/w/cpp/string/byte/tolower

http://en.cppreference.com/w/cpp/string/byte/tolower

The answer is to cast the result to a char before printing.

答案是在打印之前将结果转换为字符。

e.g.:

例如:

std::cout << static_cast<char>(std::to_lower('A'));

回答by Nikola Butigan

To lower is int so it returns int. If you check #include <ctype>you will see that definition is int tolower ( int c );You can use loop to go trough string and to change every single char to lowe case. For example

降低是 int 所以它返回 int。如果您检查,#include <ctype>您将看到该定义是int tolower ( int c );您可以使用循环遍历字符串并将每个字符更改为小写。例如

while (str[i]) // going trough string 
{
  c=str[i]; // ging c value of current char in string 
  putchar (tolower(c)); // changing to lower case      
  i++;  //incrementing 
}

回答by hmofrad

Generally speaking to convert an uppercase character to a lowercase, you only need to add 32to the uppercase character as this number is the ASCII code difference between lowercase and uppercase characters, e.g., 'a'-'A'=97-67=32.

一般来说,将大写字符转换为小写字符,只需要32在大写字符上加上,因为这个数字是小写和大写字符之间的ASCII码差异,例如,'a'-'A'=97-67=32

char c = 'B';
c += 32; // c is now 'b'
printf("c=%c\n", c);

Another easy way would be to first map the uppercase character to an offset within the range of English alphabets 0-25i.e. 'a' is index '0' and 'z' is index '25' inclusive and then remap it to a lowercase character.

另一种简单的方法是首先将大写字符映射到英文字母范围内的偏移量,0-25即 'a' 是索引 '0','z' 是索引 '25',然后将其重新映射到小写字符。

char c = 'B';
c = c - 'A' + 'a'; // c is now 'b'
printf("c=%c\n", c);