Performing Arithmetic with Symbols in C / C ++

How does this code work in C / C ++? I understood most of this, but not the part below:

c2= (c1>='a' && c1<='z') ? ('A'+c1-'a'):c1

Especially this part:

('A'+c1-'a')

What does this piece of code do?

Both c1and c2have a type char.

+4
source share
3 answers

Code converts lowercase to uppercase. If the character is not lowercase, it returns the original character.

The expression ('A'+c1-'a')performs the conversion. c1-awill give the 0-position of the character in the alphabet. By adding this value to A, you get an uppercase equidistant c1.

: c1 'b', c1-'a' 1, "b" 0 " 1 " A " " B ",

+8

:

('A'+c1-'a')

c1 .

:

c2= (c1>='a' && c1<='z') ? ('A'+c1-'a'):c1

: " c1 - , c2, c1 c2".

+2

char . .

( ) char c1 char. ASCII .

std:: toupper,

+1
source

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


All Articles