Find the Character Case Problem - Java, Python, C , C++


TABLE OF CONTENT
Problem : Find the Character Case – Test Cases
Procedure to Approach the solution
SOLUTION IN JAVA
SOLUTION IN PYTHON
SOLUTION IN C++
SOLUTION IN C

Problem : Find the Character Case -

Write a program that takes a character as an Input, and print one of the following values: 1, 0, or -1.

The program should print the value based on certain rules -

  • If the character is an uppercase alphabet (A - Z), print 1
  • If the character is a lowercase alphabet (a - z), print 0
  • If the character is not an alphabet, print -1

Input format :

Single Character

Output format :

1 or 0 or -1

Constraints :

Input can be any character


Sample Input 1 :

c

Sample Output 1 :

0


Sample Input 2 :

L

Sample Output 2 :

1


Sample Input 3 :

$

Sample Output 3 :

-1


Procedure to Approach the solution :

  1. Take an input character from the user.
  2. Check for the conditions if the given character lies between “A” and “Z”, this means we have uppercase in the input character, then print '1'. If it lies between “a” and “z”, we have lowercase in the input character, then print '0' otherwise print '-1'.

Pseudo code for the problem:

Take the input from the user.

input=ch
If ch>=‘A’ and ch<=‘Z’:
     print(1) 
If ch>= ‘a’ and ch<=‘z’:
     print(0) 
Else:
     print(-1) 

Let's dry run the code:

  • If the input is M, it satisfies the 1st condition: ch>= ‘A’ and ch<= ‘Z’, therefore, 1 will be printed.
  • If the input is m, it satisfies the 2nd condition: ch>=’a’ and ch<=’z’, therefore, 0 will be printed.
  • If the input is $, we will move to the else and -1 will be printed.

Solution in JAVA : Find the Character Case

import java.util.Scanner;
public class find_character_case {

	public static void main(String[] args) {
		Scanner s=new Scanner(System.in);
		String str=s.nextLine();
		char c=str.charAt(0);
		if (Character.isUpperCase(c)) {
			System.out.println("1");
		} else if (Character.isLowerCase(c)) {
			System.out.println("0");
		}
		else {
			System.out.println("-1");
		}
	}

}
Find the Character Case – Java Problem

Solution in PYTHON : Find the Character Case

c = input() [0] 
if ('a'<= c and c <= 'z'): 
    out = 0 
elif ('A'<= c and c <= 'Z'): 
    out = 1 
else: 
    out = -1 
print(out)

Solution in C++ : Find the Character Case

#include<iostream>
using namespace std; 

int main() {
    char ch; 
    cin >> ch; 
    if(ch >= 'A' && ch <= 'Z') { 
        cout << "1"; 
    } 
    else if(ch >= 'a' && ch <= 'z') {
        cout << "0"; 
    } 
    else{
        cout << "-1";
    }
}

Solution in C : Find the Character Case

#include <stdio.h>

int main() {
    char ch; 
    scanf("%c", &ch); 
    
    if(ch >= 'A' && ch <= 'Z') {
        printf("%d", 1); 
    } 
    else if(ch >= 'a' && ch<= 'z') {
        printf("%d", 0);
    }
    else{
        printf("%d", -1);
    }
}

You may like to Explore -

Othello Move Function – Java Code Problem Solution

Pairs with difference of K – in Java, C++ and Python

Cheers.

Happy Coding.

About the Author

This article was authored by Rawnak.

Comments are closed.