Reverse the words in String – Java

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

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”

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

 

 

 

 

Comments (0)
Add Comment