0

How to reverse each word of a given string in java?

1st Nov 2017, 11:38 PM
Vibhash Ranjan Ray
Vibhash Ranjan Ray - avatar
2 Answers
+ 4
String str = "Hello"; char[] reverse = new char[str.length()]; for(int i=str.length()-1, j=0; i>=0; i--, j++) reverse[j] = str.charAt(i); System.out.println(new String(reverse));
2nd Nov 2017, 12:52 AM
Chriptus13
Chriptus13 - avatar
+ 1
Or you can just do: String str="Hello", reverse=""; for(int i=str.length()-1; i>=0; i--) reverse+=str.charAt(i);
2nd Nov 2017, 12:53 AM
Chriptus13
Chriptus13 - avatar