In: Computer Science
In Java:
Write a nested loop that allows the user to continuously enter phrases and output underscores for each character of the phrase. End when the user enters "done" (ignoring case).
Once you have this completed, modify your code so that if the character is whitespace, you print a space " " instead of an underscore - Hint: use Character.isWhitespace(YOURCHARHERE) //returns a boolean.
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ch = sc.nextLine();
while (!ch.equalsIgnoreCase("done")) {
for (int i = 0; i < ch.length(); i++) {
System.out.print("_");
}
System.out.println();
ch = sc.nextLine();
}
}
}
Part2:
import java.util.Scanner;
public class Problem1 {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String ch = sc.nextLine();
while (!ch.equalsIgnoreCase("done")) {
for (int i = 0; i < ch.length(); i++) {
if (Character.isWhitespace(ch.charAt(i)))
System.out.print(" ");
else
System.out.print("_");
}
System.out.println();
ch = sc.nextLine();
}
}
}
Note : Please comment below if you have concerns. I am here to help you
If you like my answer please rate and help me it is very Imp for me