Write a program to accept a character from the terminal window and determine whether it is a letter or not. if it is an alphabet, display its opposite case. if it is not an alphabet, display an appropriate message.
Answers
Program {JAVA} :
Import java.util.*;
public class Check
{Scanner sc= new Scanner( System.in);
public void main()
{
System.out.println(“Enter the characher”);
char ch= sc.next().chatAt(0);
if(Character.isLetter(ch))
{
if(Character.isUpperCase(ch))
System.out.println(ch+" opposite case is "+ Character.toLowerCase(ch));
else
System.out.println(ch+ " opposite case is " +Character.toUpperCase(ch));
}
else
System.out.println(ch+" is not a letter");
}
}
include <stdio.h>
int main()
{
char ch;
//Asking user to enter the character
printf("Enter any character: ");
//storing the entered character into the variable ch
scanf("%c",&ch);
if( (ch>='a' && ch<='z') || (ch>='A' && ch<='Z'))
printf("The entered character %c is an Alphabet",ch);
else
printf("The entered character %c is not an Alphabet",ch);
return 0;
}
Output 1:
Enter any character: 9
The entered character 9 is not an Alphabet
Output 2:
Enter any character: P
The entered character P is an Alphabet
You can also check the alphabet using the ASCII values of characters like this:
if((ch >= 97 && ch <= 122) || (ch >= 65 && ch <= 90))
printf("The entered character %c is an Alphabet",ch);
else
printf("The entered character %c is not an Alphabet",ch);
The ASCII value of ‘a’ is 97, ‘z’ is 122, ‘A’ is 65 and ‘Z’ is 90.