Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
Skip to content
HowToDoInJava
  • Java
  • Spring AI
  • Spring Boot
  • Hibernate
  • JUnit 5
  • Interview

Reverse All Characters of a String in Java

Learn to reverse all the characters of a Java string using the recursion and StringBuilder.reverse() methods.

Lokesh Gupta

January 10, 2023

Java String class
Java String
Reverse String in Java using recursion

In this Java tutorial, we will learn to reverse the characters of a string using the recursion and StringBuilder.reverse() methods. You may like to read about reversing the words in a sentence also.

1. Reverse using Recursion

To reverse all the characters of the string, we can write a recursive function that will perform the following actions –

  • Take the first character and append it to the last of the string
  • Perform the above operation, recursively, until the string ends
public class ReverseString {

  public static void main(String[] args) {

    String blogName = "How To Do In Java";
    String reverseString = reverseString(blogName);

    Assertions.assertEquals("avaJ nI oD oT woH", reverseString);
  }

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

2. Reverse using StringBuilder.reverse()

We can also reverse a string easily, using a StringBuilder.reverse() method. The reverse() method causes the characters of String to be replaced by the reverse of the sequence.

String blogName = "How To Do In Java";

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

Assertions.assertEquals("avaJ nI oD oT woH", reverseString);

Happy Learning !!

Sourcecode on Github

Comments

Subscribe
Notify of
0 Comments
Most Voted
Newest Oldest
Inline Feedbacks
View all comments

String Examples

  • String Constant Pool
  • 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
  • Align Left, Right, Center
  • Immutable Strings
  • StringJoiner
  • Split a string
  • Escape HTML
  • Unescape HTML
  • Convert to title case
  • Find duplicate words
  • Left pad a string
  • Right pad a string
  • Reverse recursively
  • Leading whitespaces
  • Remove whitespaces
  • Reverse words
  • Find duplicate characters
  • Get first 4 characters
  • Get last 4 characters
  • (123) 456-6789 Pattern
  • Interview Questions

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()
Photo of author

Lokesh Gupta

A fun-loving family man, passionate about computers and problem-solving, with over 15 years of experience in Java and related technologies. An avid Sci-Fi movie enthusiast and a fan of Christopher Nolan and Quentin Tarantino.
Follow on Twitter Portfolio

Previous

Reverse Words in String without Changing Order

Next

Remove Leading / Trailing Whitespaces in Java

About Us

HowToDoInJava provides tutorials and how-to guides on Java and related technologies.

It also shares the best practices, algorithms & solutions and frequently asked interview questions.

Tutorial Series

OOP

Regex

Maven

Logging

TypeScript

Python

Meta Links

About Us

Advertise

Contact Us

Privacy Policy

Our Blogs

REST API Tutorial

Follow On:

  • Github
  • LinkedIn
  • Twitter
  • Facebook
Copyright © 2026 | Sitemap