Sunday, July 29, 2018

Program to find the case of a character


In this program, user will enter any character and the program will show whether the entered character is in upper case or in lower case.

The first program is done comparing the ASCII value of the entered character. ASCII values of 'A' to 'Z' are within 65 to 90 and ASCII values of 'a' to 'z' are within 97 to 122.

import java.io.*;
class CaseChecking

{
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char ch;

public void take() throws Exception
{
 System.out.print("\nEnter the character:");
ch=(char)br.read();
if (ch>=65 && ch<=90)
System.out.println("Entered Character is in Upper Case.");
else
System.out.println("Entered Character is in Lower Case.");
}

public static void main(String args[]) throws Exception
{
 CaseChecking ob=new CaseChecking();
ob.take();
}
}

The same program can be done using Character class static methods 'boolean isUpperCase()' or 'boolean isLowerCase()'.


import java.io.*;
class CaseChecking

{
 BufferedReader br=new BufferedReader (new InputStreamReader(System.in));
char ch;

public void take() throws Exception
{
 System.out.print("\nEnter the character:");
ch=(char)br.read();
if (Character.isUpperCase(ch))
System.out.println("Entered Character is in Upper Case.");
else
System.out.println("Entered Character is in Lower Case.");
}

public static void main(String args[]) throws Exception
{
 CaseChecking ob=new CaseChecking();
ob.take();
}
}

No comments:

Post a Comment

Subscribe via email

Enter your email address:

Delivered by FeedBurner