0
How we can reverse a string?
3 Answers
+ 3
@ASNM:
You can get it a few shortener by modifying your 'while' statement ;)
while (len--) {
reverseText+=text.charAt(len);
}
+ 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);
}
}
+ 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);
}