How to Reverse a String in Java



How to Reverse a String in Java

Sample Code

public class reverse_string {
  public static void main(String[] args) {
    String str = "WikiTechy", revstr = "";
    char ch;

    System.out.println("Original word: " + str);

    for (int i = 0; i < str.length(); i++) {
      ch = str.charAt(i);
      revstr = ch + revstr;
    }
    System.out.println("Reversed word: " + revstr);
  }
}

Output

Original word: WikiTechy
Reversed word: yhceTikiW

Related Searches to How to Reverse a String in Java