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
Arslan Ahmad
Arslan Ahmad
ex-FAANG engineering manager and author or Grokking series.
-

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
What is the best software for app development?
Which is best for aptitude test?
What is overloading in Oops?
Which Faang company has the hardest interview?
What is the salary of AWS?
What to expect in DocuSign system design interview?
Related Courses
New
Grokking the AI System Design Interview course cover
Grokking the AI System Design Interview
Learn to design AI systems the way interviewers expect: classic ML products, LLM and RAG architectures, and agentic systems, all through the lens of the system design interview.
4.8
(1,192 learners)
Discounted price for Your Region

$123

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.
4.1
Discounted price for Your Region

$72

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