ArtsAutosBooksBusinessEducationEntertainmentFamilyFashionFoodGamesGenderHealthHolidaysHomeHubPagesPersonal FinancePetsPoliticsReligionSportsTechnologyTravel

Java Program: Reverse String in Java Using For Loop

Updated on June 19, 2013

How to make a reverse string in Java using for loop?

We have a reverse string tutorial using recursion in our collection of Java Tutorials here, however I made another reverse string java program using for loop for my another Java Tutorial, the Palindrome Test. Like the reverse string in recursion, this reverse string in for loop is also a very small program. But I prefer this one than the other. It is easier to understand and to trace than the one in recursion. I bet you would also prefer this. See the code below for the verdict.

Java Source Code on Reverse String using For Loop

package reverse_string_in_java;

import java.util.Scanner;

public class Main {

    public static void main(String[] args)
    {
       Scanner input = new Scanner(System.in);

       System.out.print("Enter a String: ");
       String string = input.nextLine();

       System.out.println("Reverse String Result: ");
       for(int i=1; i<=string.length() ;i++)
       {  
            System.out.print(string.charAt(string.length()-i)); 
       }
       System.out.println();
    }

}

Sample Output:

Comments

    0 of 8192 characters used
    Post Comment

    • profile image

      boby 2 years ago

      make it more simple for students

    Click to Rate This Article