As we know string reverse in java means read string from last character to first. Reverse of a string in java is used for the reverse the string which is entered by the user. String reverse in java is used to reverse a string in simple manner. Java program to reverse a string contains a simple for loop logic. Reverse function is used for string reverse java. In the following java reverse string code we take user defined input using scanner method.
In string reverse in java first we calculate length of the string. The following program is for java code to reverse a string. In the output file shows the simple java reverse string example.
Program for string reverse in java with Example
import java.util.*; // Program of java reverse string
public static void main(String args)
String original, reverse = "";
Scanner in = new Scanner(System.in);
System.out.println("Enter a string for reverse:");
original = in.nextLine();
int length = original.length();
// logic of reverse string in java
for ( int l = length - 1 ; l >= 0 ; l-- )
reverse = reverse + original.charAt(l);
System.out.println("Reverse of entered string is: "+reverse);
} // End of java code to reverse a string