Java

Program to reverse a string entered by user.

Program to reverse a string entered by user.
import java.util.Scanner;
public class demo1 {

    public static void main(String[] args) {
        String str;
        System.out.println("Enter your username: ");
        Scanner file = new Scanner(System.in);
        str = file.nextLine();
        file.close();
        String reverse = reverseString(str);
        System.out.println("The reverse string is: " + reverse);
    }

    public static String reverseString(String str)
    {
        if (str.isEmpty())
            return str;
        return reverseString(str.substring(1)) + str.charAt(0);
    }
}
Nikhil Juneja

Passionate software developer and technical contributor at TutorialsLink.