Reverse the words in the given String

Reverse the words in the given String


Given a string S as the input Write a program to print the reverse order of the words in the string S.

Input Format:
The first line contains S.

Output Format:
The first line contains the words in the reverse order.

Boundary Conditions:
1 <= Length of S <= 100

Example Input/Output 1:
Input:
I Love India

Output:
India Love I

Example Input/Output 2:
Input:
Something must be printed!

Output:
printed! be must Something

Code:

import java.util.*;
public class Hello {
    public static void main(String[] args) {
                        Scanner sc=new Scanner(System.in);
                        String st=sc.nextLine();
                        String[] sp=st.split("\s");
                        int i;
                        for(i=sp.length-1;i>=0;i--)
                            System.out.print(sp[i]+" ");
            }
}

#v2#v2lancers#v2lancer

Add a Comment

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