[Solved] YODA String with Java

Description: As a young jedi you must learn to converse with Yoda. You have found a simple rule that helps change a “normal” sentence into “Yoda talk”. Take the first two words in the sentence and place them at the end. Write a program that uses this rule to change normal sentence into “Yoda talk”.

Input:

Input consists of a string that you must change into “Yoda talk”.

Assume that the maximum length of the string is 100;

Output:

Print the corresponding sentence in Yoda talk.

Sample Input:

I will go now to find the Wookiee

Sample Output:

go now to find the Wookiee I will

Solution

import java.util.Scanner;

public class Main {
    public static void main(String[] args) {
        Scanner s = new Scanner(System.in);
        String str1, sentence = "";
        str1 = s.nextLine();
        String[] str2 = str1.split("\\s");
        for (int i = 2; i < str2.length; i++) {
            sentence += str2[i] + " ";
        }
        sentence += str2[0] + " " + str2[1];
        System.out.println(sentence);
    }
}

Happy Learning – If you require any further information, feel free to contact me.

Share your love
Saurav Hathi

Saurav Hathi

I'm currently studying Bachelor of Computer Science at Lovely Professional University in Punjab.

📌 Nodejs and Android 😎
📌 Java

Articles: 444

Leave a Reply

Your email address will not be published. Required fields are marked *