Java Program 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 reverse to store the reverse of the original string).
import java.util.*;
public class ReverseString
{
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);
}
}
OUTPUT ::
Enter a string to reverse
CodezClub
Reverse of entered string is: bulCzedoC
SOURCE CODE ::
OUTPUT ::
need an explanation for this answer? contact us directly to get an explanation for this answer