Sunday, September 16, 2018

Java Program of Reverse a String

Here we will reverse a string i.e Last word print at first place and first-word print at last place. (Not reverse with the character of each word).


public class ReverseString {

            public static void main(String[] args) {
                       
                        String str = "Java is a simple language";
                       
                        String[] words= str.split(" ");
                        String reverseword = "";
                        for(int i=words.length-1; i>=0; i--)
                       
                        {
                         reverseword = reverseword + words[i] + " ";
                                               
                        }
                       
                        System.out.println("Reverse Word: "+ reverseword);
                       
            }
           
             

}




Output:  Reverse Word: language simple a is Java 






No comments:

Post a Comment