Reverse words in a given string

Given a String of length N reverse the whole string without reversing the individual words in it. Words are separated by dots.

Code:

package sap;

public class ReverseWords {
   
    public static void main(String[] args){
       
        String word = "I am done";
        StringBuilder result = new StringBuilder("");
       
        String[] arr = word.split("\\s");
       
        for(int i=arr.length-1; i>=0; i--){
           
            result.append(arr[i]);
            result.append(" ");
           
        }
       
        System.out.println(result.toString());
       
       
    }

}
 



Output:
done am I
 

Comments

Popular posts from this blog

Rearrange Array in Maximum-Minimum form

Second Largest Element

Check if a number is a power of another number