Program to convert a lowercase character to uppercase using conditional operator in C/C++

LEVEL: Beginner

Whats in this post?

  1. Code
  2. Output
  3. Background and Explanation of the code
  4. Download Code


To copy code:¬†Click ¬†View Source “View Source” on the right of this code snippet.

Background and Explanation of the code

1.In this program we first enter a character .

2.Now we check whether the input character is a lowercase character. (‘a’ and ‘z’ means that we are actually referring to the ASCII values of ‘a’ and ‘z’ and checking whether the input character lies in the ASCII range between ‘a’ and ‘z’)

3. Now, if the character is a lowercase character, we assign its corresponding upper case character in the variable ‘c2′ by adding the ASCII value of capital ‘A’ (65) to the input character and subtracting the ASCII value of small ‘a’ from this. This returns the corresponding uppercase character for the given lowercase character. But if the input character is already lowercase, we return to variable ‘c2′, the input character without any manipulation.

Example: Suppose lowercase character (s) is entered. Then the condition of the expression is satisfied and we get ‘A’+’s’-‘a’ = S as answer.


Enter a character : s
Uppercase Equivalent = S