c++ - tolower() is returning a number instead of a the lowercase form? -
i writing program asks input of letters , and sorts them letter , occurrence based on input. i'm @ end of code , trying convert uppercase letters lowercase. i'm trying this:
cout << tolower(char('a'+i)) << " " << alphabets[i] <<endl;
but tolower()
outputs number instead of lowercase version of letter? example, input "aaaa"
gives me :
97 4
and input "bbbbb"
gives me:
98 5
but when take out tolower
, input "aaa"
be:
a 3
i don't understand why happening.
tolower
ancient function inherited 1970's-era c, before function signatures reliably existed , before people cared different integer types expressions.
so, ignores types , returns character in int
. 3 alternatives:
- cast:
static_cast< char >( std::tolower( 'a' + ) )
. - use variable:
char lower = std::tolower( 'a' + );
- use newer overload:
std::tolower( 'a', std::locale() )
.
Comments
Post a Comment