HowToDoInJava

  • Python
  • Java
  • Spring Boot
  • Dark Mode
Home / Java / String / Java reverse string using recursion

Java reverse string using recursion

In this post, we will see a Java program to reverse a string using recursion. Also learn to reverse string without recursion as well.

Read More: Reverse words in a string in Java

1. Reverse String using recursion

To reverse a string by characters, we can write a recursive function which will perform following actions –

  1. Take first character and append to last of string
  2. Perform above operation until string ends
public class StringExample 
{
	public static void main(String[] args) 
	{
		String blogName = "How To Do In Java ";
		
		String reverseString = reverseString(blogName);
		
		System.out.println(reverseString);
	}
	
	public static String reverseString(String string)
	{
		if (string.isEmpty()){
		 return string;
		}
		//Calling function recursively
		return reverseString(string.substring(1)) + string.charAt(0);
	}
}

Program output.

avaJ nI oD oT woH

2. Reverse a sentence in Java – without recursion

You can reverse a string by character easily, using a StringBuilder.reverse() method.

public class StringExample 
{
	public static void main(String[] args) 
	{
		String blogName = "How To Do In Java";

		String reverseString = new StringBuilder(string).reverse();

		System.out.println(reverseString);
	}
}

Program output.

avaJ nI oD oT woH

In this example, you learned to reverse a string using recursive function and also without using recursive function as well.

Happy Learning !!

Was this post helpful?

Let us know if you liked the post. That’s the only way we can improve.
TwitterFacebookLinkedInRedditPocket

About Lokesh Gupta

A family guy with fun loving nature. Love computers, programming and solving everyday problems. Find me on Facebook and Twitter.

Feedback, Discussion and Comments

  1. abc

    August 26, 2019

    Hey!
    There is a problem with “Reverse a sentence -without recursion”.

    There are casting errors.Why don’t you give it a try?

    • nehan

      August 29, 2019

      yes, there is casting error. I did it this way and it worked

      public class test
      {
      public static void main(String[] args)
      {
      String blogName = “How To Do In Java”;

      StringBuilder reverseString = new StringBuilder(blogName).reverse();

      System.out.println(reverseString);
      }
      }

Comments are closed on this article!

Search Tutorials

String methods

  • String concat()
  • String hashCode()
  • String contains()
  • String compareTo()
  • String compareToIgnoreCase()
  • String equals()
  • String equalsIgnoreCase()
  • String charAt()
  • String indexOf()
  • String lastIndexOf()
  • String intern()
  • String split()
  • String replace()
  • String replaceFirst()
  • String replaceAll()
  • String substring()
  • String startsWith()
  • String endsWith()
  • String toUpperCase()
  • String toLowerCase()

String examples

  • Convert String to int
  • Convert int to String
  • Convert String to long
  • Convert long to String
  • Convert CSV String to List
  • Java StackTrace to String
  • Convert float to String
  • String – Alignment
  • String – Immutable
  • String – StringJoiner
  • Java – Split string
  • String – Escape HTML
  • String – Unescape HTML
  • String – Convert to title case
  • String – Find duplicate words
  • String – Left pad a string
  • String – Right pad a string
  • String – Reverse recursively
  • String – Leading whitespaces
  • String – Trailing whitespaces
  • String – Remove whitespaces
  • String – Reverse words
  • String – Find duplicate characters
  • String – Check empty string
  • String – Get first 4 characters
  • String – Get last 4 characters
  • String – (123) 456-6789 pattern
  • String – Interview Questions

Java Tutorial

  • Java Introduction
  • Java Keywords
  • Java Flow Control
  • Java OOP
  • Java Inner Class
  • Java String
  • Java Enum
  • Java Collections
  • Java ArrayList
  • Java HashMap
  • Java Array
  • Java Sort
  • Java Clone
  • Java Date Time
  • Java Concurrency
  • Java Generics
  • Java Serialization
  • Java Input Output
  • Java New I/O
  • Java Exceptions
  • Java Annotations
  • Java Reflection
  • Java Garbage collection
  • Java JDBC
  • Java Security
  • Java Regex
  • Java Servlets
  • Java XML
  • Java Puzzles
  • Java Examples
  • Java Libraries
  • Java Resources
  • Java 14
  • Java 12
  • Java 11
  • Java 10
  • Java 9
  • Java 8
  • Java 7

Meta Links

  • About Me
  • Contact Us
  • Privacy policy
  • Advertise
  • Guest and Sponsored Posts

Recommended Reading

  • 10 Life Lessons
  • Secure Hash Algorithms
  • How Web Servers work?
  • How Java I/O Works Internally?
  • Best Way to Learn Java
  • Java Best Practices Guide
  • Microservices Tutorial
  • REST API Tutorial
  • How to Start New Blog

Copyright © 2020 · HowToDoInjava.com · All Rights Reserved. | Sitemap

  • Sealed Classes and Interfaces