Challenge: Mirror Each Word

Lab Java Beginner 10 points

Read a line of words separated by single spaces. Reverse the letters of each word but keep the words in their places.

Input

One line of words.

Output

The same words, each one reversed, separated by single spaces.

Example

Input

hello world

Output

olleh dlrow
Sample input available Sample output available
Editor
Java

Hints

Split the line into words.

new StringBuilder(word).reverse() flips one word.

Solution explanation

Each character is touched a couple of times: O(n) for a line of n characters.

Show solution
import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner sc = new Scanner(System.in);
        String[] words = sc.nextLine().trim().split("\\s+");
        StringBuilder sb = new StringBuilder();
        for (int i = 0; i < words.length; i++) {
            if (i > 0) sb.append(' ');
            sb.append(new StringBuilder(words[i]).reverse());
        }
        System.out.println(sb);
    }
}
Output
Answer
HOME LABS COMMUNITY DASHBOARD