Reverse the words in String – Java

Write a program to Reverse the words in given strings using java

- Advertisement -

Here we will reverse only the words in the string and keeping the string word order intact. Example:

“Hello World” will be converted as “olleH dlroW”

- Advertisement -

package Edureka;
public class stringreverse {
public static void main(String[] args) {
// TODO Auto-generated method stub
String str = "Hello Rootuser";
String[] strArray = str.split(" ");
System.out.println("given String:" + str);
for(int i=0; i<3; i++){ char[] s1 = strArray[i].toCharArray(); for (int j = s1.length-1; j>=0; j--)
{System.out.print(s1[j]);}
System.out.print(" ");
}
}
}

 

Output:

given string: Hello Rootuser

olleH resutooR

 

 

 

 

Leave A Reply

Your email address will not be published.

This website uses cookies to improve your experience. We'll assume you're ok with this, but you can opt-out if you wish. AcceptRead More