Wednesday, 4 January 2017

Write a Java Program to Reverse Each Word of Given String ?

Here we will discuss How to write a program to reverse each word of given string. Suppose for Example we have input string as "Pushkar is good boy" then output should be "rakhsuP si doog yob".

Split the given string using split() method of java.lang.String; class then take each word , reverse it and append it to the separate variable. And finally print that variable. As shown below.

Example : 

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
public class ReverseEachWordOfString {
 
 public static void getReverse(String str){
  String[] count = str.split(" ");
  String reverse="";
  for(int i=0;i<count.length;i++){
   for(int j=count[i].length()-1;j>=0;j--){
    reverse+= count[i].charAt(j);
   }
   reverse+=" ";
  }
  System.out.println("REVERSE STRING : "+reverse);
 }
 public static void main(String[] args) {
  ReverseEachWordOfString.getReverse("Pushkar is good boy");
 }
}

Output :

REVERSE STRING : rakhsuP si doog yob 




      
Blog Author - Pushkar Khosla,
Software Developer by Profession with 3.0 Yrs of Experience , through this blog i'am sharing my industrial Java Knowledge to entire world. For any question or query any one can comment below or mail me at pushkar.itsitm52@gmail.com.

This blog is all about to learn Core Java ,Interview Programs and Coding tricks to polish your Java Knowledge. If you like the content of this blog please share this with your friends.



Share this Blog with yours Friends !!

No comments:

Post a Comment