简体   繁体   中英

Converting upper and lower case letters

In c++, how do I convert a capital letter to a small letter and back?

ch= ch-('a'-'A') a -> A
ch= ch+('a'-'A'). A-> a

Is there a "formula" like that one for converting a letter to a number and reversed?

There are "formulas" to convert a lower case to upper case and vice versa. They deal with adding or subtracting the the value of distance between 'A' and 'a' . Instead of using them though we have std::tolower and std::toupper which will handle the conversion.

Converting a digit as a character to it's integer value can be done by ch - '0' - although I'm not 100% sure that's what you are asking for. Obviously, converting a string to a number means picking off the digits in the correct order and multiplying, to make the "whole" number.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM