Write a program that requests the user to enter an alphabetic character. Then co
ID: 3799272 • Letter: W
Question
Write a program that requests the user to enter an alphabetic character. Then convert the character from upper case to lower case or lower case to upper case. Print an error message if the user enters something other than an alphabetic character.
For example:
Enter an alphabetic character: A
Your input A converted to lower case is a.
Enter an alphabetic character: t
Your input t converted to upper case is T.
Enter a single digit or an alphabetic character: 9
You entered 9, which is not a letter.
Explanation / Answer
Here is the java code
import java.util.Scanner;
public class CheckCase
{
public static void main(String[] a)
{
char c;
String l;
Scanner sc= new Scanner(System.in);
System.out.println("Enter an alphabetic character: ");
l=sc.next();
c=l.charAt(0);
if(Character.isUpperCase(c))
System.out.println("Your input "+c+" converted to lower case is "+Character.toLowerCase(c));
else if(Character.isLowerCase(c))
System.out.println("Your input "+c+" converted to upper case is "+Character.toUpperCase(c));
else
System.out.println("You entered "+ c+", which is not a letter.");
}
}
// c++ code
#include <stdio.h>
#include <ctype.h>
main()
{
char c,e;
cout << "Enter an alphabetic character: ";
cin >> c;
if (isupper(c))
{
e=tolower(c);
cout << "Your input "<<c<<" converted to lower case is "<<e<<endl;
}
else if(islower(c))
{
e=toupper(c);
cout<< "Your input "<<c<<" converted to upper case is "<<e<<endl;
}
else
cout << "You entered "<< c<<", which is not a letter.";
}
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.