• Skip to main content
  • Skip to secondary menu
  • Skip to primary sidebar
  • Skip to footer
  • Home
  • Contact Us

All about code

  • Java
    • Spring
      • Spring Boot
      • Spring Cloud
    • Java Miscellaneous Tips
    • Java Design Patterns
  • Data Structures and Algorithms
    • Algorithms
    • Data Structures
  • Thoughts and Stories
  • AI

How to Reverse a String

laptop infographic online business 6087062

Introduction

We can reverse a string in different ways. We will describe two possible algorithms in this article. We will implement them in Java.

Reverse a String Problem

A String is a sequence of characters. We want to obtain a new String with a sequence reversed.

For example, imagine we have the following String:

String toReverse = "abcdef";

We want to obtain:

"fedcba"

Reverse a String by a Trivial Algorithm

A trivial solution that comes to mind is:

  • We create a new array of characters with the same length as the original one
  • We traverse the original array starting from the last element and copy each element in the second array starting from its beginning

The Java implementation of the above algorithm is shown in the following JUnit test case:

@Test
public void trivialReverseStringTest() throws Exception {

    String toReverse = "abcdef";
    char[] chArray = toReverse.toCharArray();
    char[] chArrayReversed = new char[chArray.length];

    for (int i = 0, j = chArray.length - 1; i < chArray.length; i++, j--) {
        chArrayReversed[i] = chArray[j];
    }

    System.out.println(chArrayReversed);
    for (int i = 0, j = chArray.length - 1; i < chArray.length; i++, j--) {
        assertEquals(chArrayReversed[i], chArray[j]);
    }

}

Performance Considerations

The time complexity of the above algorithm is O(n) and requires an additional O(1) space due to the use of a new array of characters with the same length as the original one.

Reverse a String in a More Efficient Way

We can improve the algorithm described in the above section, without needing an additional array. This solution comes by reconsidering the previous algorithm a little differently. We pretend we still have an additional array by progressively and simultaneously copying the left values in the right positions and the right values in the left ones.

When we arrive at the middle of the array we stop. If the length of the array is an odd integer the center position will not be affected. We can summarize this algorithm like this:

  • We start a loop from the first position on the left onwards and at the same time from the last position on the right backwards.
  • In each step of the loop, we exchange the left and right values
  • We increment the left and decrease the right index of the loop by one until the left index is less than the right one.

The following test shows an implementation of the above description:

@Test
public void reverseStringTest() throws Exception {

    String toReverse = "abcdef";
    char[] chArray = toReverse.toCharArray();

    int i = 0, j = chArray.length - 1;
    while (i < j) {
        char temp = chArray[i];
        chArray[i] = chArray[j];
        chArray[j] = temp;
        ++i;
        --j;
    }

    String reversed = String.valueOf(chArray);
    System.out.println(reversed);
    for (int n = 0, m = chArray.length - 1; n < chArray.length; n++, m--) {
        assertEquals(toReverse.toCharArray()[n], chArray[m]);
    }

}

Conclusion

In this article, we have seen how to reverse a String in Java using two different algorithms, improving the space efficiency in the second one. The example code is available on GitHub.

Primary Sidebar

Social

  • Facebook
  • LinkedIn
  • Twitter

Archives

  • May 2026
  • April 2026
  • March 2026
  • August 2025
  • July 2025
  • June 2025
  • May 2025
  • April 2025
  • March 2025
  • December 2024
  • October 2024
  • August 2024
  • July 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • September 2023
  • May 2023
  • March 2023
  • January 2023
  • November 2022
  • September 2022
  • August 2022

Recent Posts

  • Better Avoid Spring Boot Lazy-initialization @PostConstruct Trap
  • How To Handle Large Datasets in Spring Boot
  • How to Log HTTP Incoming Requests in Spring Boot
  • How to Reliably Implement Post-Commit Actions in Spring
  • Brief and Easy Explanation of HATEOAS

TAGS

Algorithms Apache POI Backtracking Date Structures Dynamic Programming engineering Graphs Greedy Horror Stories HSSF Java Java New Features Java Principles Java tips real life Sorting Spring Boot Spring Cloud Strings Trees

Footer

Privacy Policy Cookie Policy Terms and Conditions