0

How we can reverse a string?

31st Jan 2017, 11:50 AM
milad
milad - avatar
3 Answers
+ 3
@ASNM: You can get it a few shortener by modifying your 'while' statement ;) while (len--) { reverseText+=text.charAt(len); }
31st Jan 2017, 12:50 PM
visph
visph - avatar
+ 1
public class Program { public static void main(String[] args) { String text="Hello World"; String reverseText=""; int len=text.length(); while (len>0){ len--; reverseText +=text.charAt(len); } System.out.println(reverseText); } }
31st Jan 2017, 12:16 PM
ASNM
ASNM - avatar
+ 1
You can use a simple "for" loop to iterate from the back of the string to the front. String test = "Test"; String rev = ""; for (int i = test.length(); i>0; i--) { rev += test.charAt(i); }
31st Jan 2017, 5:35 PM
NickB
NickB - avatar