Character converting funtion std::isupper() & std::islower() C++17

std::tolower and std::toupper return int, not char (due to it's legacy origin from Cthere are certain requirements due to which int was chosen, see footnote).

You can cast it back to char to get expected results:

static_cast<char>(std::tolower(letter));

Or you could save the result in a char variable before (if you need that converted latter elsewhere):

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. 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. 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. It is part of the machinery of being able to read a single character from a file and deal with error conditions.


1. option

You can use std::tolower and std::toupper from <locale> header that return the type you would expect them to return.

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

You can use std::tolower and std::toupper from <cctype> header that return int that you need to cast to 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

Tags:

C++

C++17