Convert single character to lowercase in C ++ - tolower returns an integer

I am trying to convert a string to lowercase, and treat it as char * and iterate through each index. The problem is that the function tolowerI read on the Internet does not actually convert char to lowercase: it takes char as input and returns an integer.

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

prints 116to the console when it should print T.

Is there a better way to convert a string to lowercase? I looked on the Internet, and most sources say that “use tolowerand iterate through a char array”, which does not seem to work for me.

So my two questions:

  • What am I doing wrong with a function tolowerthat forces it to return 116 instead of "t" when calledtolower('T')

  • Are there any better ways to convert a string to lowercase in C ++, other than using tolowerfor each individual character?

+4
source share
5 answers

This is because there are two different functions tolower. The one you use, this one that returns int. That is why it prints 116. This is the ASCII value 't'. If you want to print char, you can simply return it back to char.

, , :

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

:

++, tolower ?

.

+8

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

, unsigned char - , char .

, - :

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

, ? . "ß" , .

+2

int, int. #include <ctype>, , int tolower ( int c );. , char .

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 
}
+1

116 , , std::cout , char(tolower(c))

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

int to_lower(int ch) , ch unsigned char EOF ( -1, ).

, c int. :

  • C int ( ).

  • EOF, , char, , .

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

, char .

:.

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

Source: https://habr.com/ru/post/1616894/


All Articles