Program to reverse a string

Program to reverse a string

import java.util.Scanner;
public class JavaExample {

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

    public static String reverseString(String str)
        if (str.isEmpty())
            return str;
        //Calling Function Recursively
        return reverseString(str.substring(1)) + str.charAt(0);

Share this

Related Posts

Next Post »