Find Character Case

Easy
0/40
Average time to solve is 9m
27 upvotes
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.
Detailed explanation ( Input/output format, Notes, Images )
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
Sample Input 1:
2
A
t
Sample Output 1:
1
0
Explanation For Sample Input 1:
For sample case 1, ‘A’ is an uppercase letter, hence output is 1.
For sample case 2, ‘t’ is a lowercase letter, hence output is 0.
Sample Input 2:
2
#
P
Sample Output 2:
-1
1
Explanation For Sample Input 2:
For sample case 1, ‘#’ is not an alphabet, hence output is -1.
For sample case 2, ‘P’ is an uppercase letter, hence output is 1.
Hint

Can you check in which range of characters ‘CH’ lie to check its type?

Approaches (1)
Brute Force

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’.
Time Complexity

O(1)

 

Since no loop is used, total time complexity is O(1).

Space Complexity

O(1)

 

Since, we are not using any extra space for solving this problem, effective space complexity is O(1).

Code Solution
(100% EXP penalty)
Find Character Case
Full screen
Console