Java program to reverse a string

0 abhishek phukan · January 5, 2015
import java.util.*;
 
class user{
   public static void main(String args[])
   {
      String original, reverse = "";
      Scanner in = new Scanner(System.in);
      System.out.println("Enter a string to reverse");
      original = in.nextLine();
      int length = original.length();
      for ( int i = length-1  ; i >= 0 ; i-- )
      reverse = reverse + original.charAt(i);
 
      System.out.println("Reverse of entered string is: "+reverse);
   }
}

Please can someone explain why in the for loop the length of the string is deducted by 1...can anybody explain the following line:
for(int i =length-1;......);
Thank You.

Post a Reply

Replies

Oldest  Newest  Rating
0 abhishek phukan · January 5, 2015
Thanx Sam...
  • 1

Java / Android Development

103,694 followers
About

Very popular language used to create desktop applications, website applets, and Android apps.

Links
Moderators
Bucky Roberts Administrator