DO IT IN JAVA Write a program which continually asks the user to enter a single
ID: 3797308 • Letter: D
Question
DO IT IN JAVA
Write a program which continually asks the user to enter a single sentence on a line by itself. For each position in the string, alternate uppercase and lowercase letters using a for loop. The user will enter an empty string to quit. a) Logic controlling the asking for and entering of text b) the for loop for manipulating the string c) the logic involved in manipulating the string d) the outer loop for managing the continual input e) the check for user entering an empty string f) Name your program: CrazyString.javaExplanation / Answer
CrazyString.java
import java.util.Scanner;
public class CrazyString {
public static void main(String[] args) {
Scanner scan = new Scanner(System.in);
System.out.println("Enter the sentence(empty to exit): ");
String sentence = scan.nextLine();
while(!sentence.trim().equals("")){
for(int i=0; i<sentence.length(); i++){
if(i % 2 == 0){
System.out.print(Character.toLowerCase(sentence.charAt(i)));
}
else{
System.out.print(Character.toUpperCase(sentence.charAt(i)));
}
}
System.out.println();
System.out.println("Enter the sentence(empty to exit): ");
sentence = scan.nextLine();
}
}
}
Output:
Enter the sentence(empty to exit):
Hi, good morning. welcome to the program
hI, gOoD MoRnInG. wElCoMe tO ThE PrOgRaM
Enter the sentence(empty to exit):
Good evening
gOoD EvEnInG
Enter the sentence(empty to exit):
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.