Sunday, 16 August 2015

Java : Reverse a String

Wings of Fire: An Autobiography

Code to reverse string is as follows -

public class StringReverse {

  public static void main(String[] args) {

      String original, reverse =""; // declare two string variable

      original = "Sample String"; //String to be reversed

      int length = original.length(); //find length of string

      for ( int i = length - 1 ; i >= 0 ; i-- )

         reverse = reverse + original.charAt(i); //charAt returns one char at a time & assign to reverse 

      System.out.println("Reverse is :- "+reverse); //print reverse of string

  }
}

No comments:

Post a Comment