Letter Flip Your task is to write a program to reverse the case of the letters i
ID: 3744220 • Letter: L
Question
Letter Flip Your task is to write a program to reverse the case of the letters in a line. If a letter is lowercase in the input, it should be uppercase in the output, and vice versa. Input Each test case will consist of a single line. Each line will consist of at most 80 characters. Input will be terminated by a line containing just the word "STOP." Sample input: Here is a 1ine. Here is another line STOP that STOP Output For each test case, there should be precisely one line of output. This line should contain the original input line, except with the cases of all letter characters reversed. Sample output hERE IS A LINE hERE IS ANOTHER LINE. stop THATIExplanation / Answer
Below is the solution:
import java.util.Scanner;
public class Reverse {
public static void main(String[] args) {
String str1;
String[] str2 = new String[80];
String[] str3 = new String[80];
Scanner read = new Scanner(System.in);
System.out.println("Enter string to reverse:");
int n = 0;
do { //input a string untill type stop
str1 = read.nextLine(); //read string
str2[n] = str1;
n++;
} while (!str1.toLowerCase().equals("stop"));
for (int j = 0; j < n - 1; j++) { //loop to
String reverse = "";
for (int i = str2[j].length() - 1; i >= 0; i--) {
reverse = reverse + str2[j].charAt(i); //reverse a string
}
str3[j] = reverse; //put the reverse string in one line at str3 string
}
for (int j = 0; j < n - 1; j++) {
System.out.print(" ");
for (int i = 0; i<str2[j].length(); i++) {
char aChar = str2[j].charAt(i); //single charceter in aChar variable
if (65 <= aChar && aChar<=90) //check if the aChar is lower
{
aChar = (char)( (aChar + 32) ); //convert into upper
}
else if (92 <= aChar && aChar<=122) //check if the aChar is upper
{
aChar = (char)( (aChar - 32) ); //convert into lower
}
System.out.print(aChar); //print
}
}
System.out.print(" ");
for (int j = 0; j < n - 1; j++) {
System.out.print(" ");
for (int i = 0; i<str3[j].length(); i++) {
char aChar = str3[j].charAt(i); //single charceter in aChar variable
if (65 <= aChar && aChar<=90) //check if the aChar is lower
{
aChar = (char)( (aChar + 32) ); //convert into upper
}
else if (92 <= aChar && aChar<=122) //check if the aChar is upper
{
aChar = (char)( (aChar - 32) ); //convert into lower
}
System.out.print(aChar); //print
}
}
}
}
output:
Enter string to reverse:
Here is line.
Here is another line.
ThinGs
STOP that!
stop
hERE IS LINE.
hERE IS ANOTHER LINE.
tHINgS
stop THAT!
.ENIL SI EREh
.ENIL REHTONA SI EREh
SgNIHt
!TAHT pots
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.