What is the best way to reverse a string?

Best Ways to Reverse a String

Reversing a string is a common programming task that can be accomplished in several ways, depending on the programming language you are using. Here are some of the most efficient and straightforward methods to reverse a string in various popular programming languages.

Python

In Python, you can reverse a string using slicing, which is both simple and efficient.

Method 1: Slicing

def reverse_string(s): return s[::-1] # Example usage print(reverse_string("hello")) # Output: "olleh"

Method 2: Using reversed() and join()

def reverse_string(s): return ''.join(reversed(s)) # Example usage print(reverse_string("hello")) # Output: "olleh"

Java

In Java, you can reverse a string using the StringBuilder or StringBuffer class, which provides a built-in reverse() method.

Method 1: Using StringBuilder

public class Main { public static String reverseString(String s) { return new StringBuilder(s).reverse().toString(); } public static void main(String[] args) { System.out.println(reverseString("hello")); // Output: "olleh" } }

Method 2: Using a Loop

public class Main { public static String reverseString(String s) { char[] chars = s.toCharArray(); int left = 0, right = s.length() - 1; while (left < right) { char temp = chars[left]; chars[left] = chars[right]; chars[right] = temp; left++; right--; } return new String(chars); } public static void main(String[] args) { System.out.println(reverseString("hello")); // Output: "olleh" } }

JavaScript

In JavaScript, you can reverse a string using the split(), reverse(), and join() methods.

Method 1: Using split(), reverse(), and join()

function reverseString(s) { return s.split('').reverse().join(''); } // Example usage console.log(reverseString("hello")); // Output: "olleh"

C++

In C++, you can reverse a string using the std::reverse algorithm from the <algorithm> library.

Method 1: Using std::reverse

#include <iostream> #include <algorithm> #include <string> std::string reverseString(const std::string& s) { std::string reversed = s; std::reverse(reversed.begin(), reversed.end()); return reversed; } int main() { std::cout << reverseString("hello") << std::endl; // Output: "olleh" return 0; }

Method 2: Using a Loop

#include <iostream> #include <string> std::string reverseString(const std::string& s) { std::string reversed = s; int left = 0, right = s.length() - 1; while (left < right) { std::swap(reversed[left], reversed[right]); left++; right--; } return reversed; } int main() { std::cout << reverseString("hello") << std::endl; // Output: "olleh" return 0; }

Summary

  • Python: Use slicing (s[::-1]) or reversed() with join().
  • Java: Use StringBuilder's reverse() method or a manual loop.
  • JavaScript: Use split(), reverse(), and join().
  • C++: Use std::reverse from the <algorithm> library or a manual loop with std::swap.

Each method is idiomatic to the language it is used in and provides an efficient way to reverse a string. For more in-depth knowledge and practical examples on programming concepts, consider exploring Grokking the Coding Interview on DesignGurus.io, which provides comprehensive courses on essential coding and interview techniques.

TAGS
Coding Interview
CONTRIBUTOR
Design Gurus Team
-

GET YOUR FREE

Coding Questions Catalog

Design Gurus Newsletter - Latest from our Blog
Boost your coding skills with our essential coding questions catalog.
Take a step towards a better tech career now!
Explore Answers
Which is the best YouTube channel to learn DSA?
What to say in an Amazon interview?
Why Netflix interview questions?
What is oops in C++?
Which MongoDB interview questions to prepare github?
What are your biggest weakness answers?
Related Courses
Grokking the Coding Interview: Patterns for Coding Questions course cover
Grokking the Coding Interview: Patterns for Coding Questions
The 24 essential patterns behind every coding interview question. Available in Java, Python, JavaScript, C++, C#, and Go. The most comprehensive coding interview course with 543 lessons. A smarter alternative to grinding LeetCode.
4.6
Discounted price for Your Region

$197

Grokking Modern AI Fundamentals course cover
Grokking Modern AI Fundamentals
Master the fundamentals of AI today to lead the tech revolution of tomorrow.
3.9
Discounted price for Your Region

$72

Grokking Data Structures & Algorithms for Coding Interviews course cover
Grokking Data Structures & Algorithms for Coding Interviews
Unlock Coding Interview Success: Dive Deep into Data Structures and Algorithms.
4
Discounted price for Your Region

$78

Design Gurus logo
One-Stop Portal For Tech Interviews.
Copyright © 2026 Design Gurus, LLC. All rights reserved.