We have single character Chr
on input. Output must be lowercase of Chr
if Chr
is uppercase and vice versa.
Trivial realization using if else
statement:
if(Chr>='a' && Chr<='z') cout<<(unsigned char)(a-32);
else cout<<(unsigned char)(a+32);
Could you propose solution without any condition branching?
Something like Chr + 32*(<sign of>(Chr - 'a'))
?
Update: I kept ASCII in mind when asked this question.
If your characters are using the ASCII character set you can xor with the value 32 to flip between upper and lower case.
char switchCase(char letter)
{
return letter ^ 32;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With