Reverse Each Word
Completed
Write static String reverse(String s). Read a line and print it with every word reversed but the word order kept.
Input
One line.
Output
One line.
Example
Input
Hello Java World
Output
olleH avaJ dlroW
Editor
Java
Hints
Split into words, reverse each with your method, join them back.
Solution explanation
Small helper methods keep main short and readable.
Show solution
import java.util.Scanner;
public class Main {
static String reverse(String s) {
return new StringBuilder(s).reverse().toString();
}
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String[] words = sc.nextLine().trim().split("\\s+");
StringBuilder sb = new StringBuilder();
for (String w : words) {
sb.append(sb.length() > 0 ? " " : "").append(reverse(w));
}
System.out.println(sb);
}
}