Last Updated: 25 Nov, 2020

Find Character Case

Easy
Asked in company
Nagarro Software

Problem statement

You are given a character ‘CH’ as input, return either 1, 0 or -1 according to the following rules:

1, if the character is an uppercase alphabet (A - Z).

0, if the character is a lowercase alphabet (a - z).

-1, if the character is not an alphabet.

For Example :
If ‘CH’ = ‘a’, then since it is a lowercase letter, your program should return 0.
Input Format :
The first line contains a single integer T representing the number of test cases.

The first line and only line of each test case contains the character ‘CH’.
Output Format :
For each test case, print 1 or 0 or -1 as discussed above.

Output for every test case will be printed in a separate line.
Note:
You don’t need to print anything. It has already been taken care of.
Constraints:
1 <= T <= 10 
‘CH’ = ASCII character   

Time Limit: 1 sec

Approaches

01 Approach

The idea is to simply check in what range the character ‘CH’ lies to find its type. For example, lowercase letters must lie in the range of ‘a’ to ‘z’ and uppercase letters must lie in the range of ‘A’ to ‘Z’.

 

Here is the algorithm:

  1. Declare integer variable ‘ANS’. This will store our answer.
  2. If ‘CH’ >= ‘A’ and ‘CH’ <= ‘Z’, it must be an uppercase letter:
    • ‘ANS’ = 1.
  3. Else if ‘CH’ >= ‘a’ and ‘CH’ <= ‘z’, it must be a lowercase letter:
    • ‘ANS’ = 0.
  4. Else (‘CH’ is not an alphabet):
    • ‘ANS’ = -1.
  5. Finally, return ‘ANS’.