To reverse any string in Java Programming, you have to ask to the user to enter the string and start placing the character present at the last index of the original string in the first index of the reverse string (make a variable say rev to store the reverse of the original string).
Following Java Program ask to the user to enter a string to reverse it and display the reversed string on the screen:
/* Java Program Example - Reverse a String */ import java.util.Scanner; public class JavaProgram { public static void main(String args[]) { String orig, rev=""; int i, len; Scanner scan = new Scanner(System.in); System.out.print("Enter a String to Reverse : "); orig = scan.nextLine(); len = orig.length(); for(i=len-1; i>=0; i--) { rev = rev + orig.charAt(i); } System.out.print("Reverse of Entered String is : " +rev); } }
When the above Java Program is compile and executed, it will produce the following output:
You may also like to learn and practice the same program in other popular programming languages: