Whether you are learning your first programming language or prepping for a technical interview, string reversal is one of those classic problems you cannot avoid. It looks simple on the surface, but the variety of ways to solve it makes it an excellent playground for learning programming concepts. This article will take you through different ways to reverse a string using various languages and approaches, from the beginner friendly to the more advanced. By the end, you will not only know how to reverse a string but also understand the logic behind each method.
And if you are serious about mastering programming, check out the Full Stack Development Course by Uncodemy. It gives you hands on training in real world projects and builds a strong foundation in logic and problem solving.

Before we dive into code, let us understand what string reversal means. A string is just a sequence of characters. Reversing it means flipping the order of characters. For example:
Original: hello
Reversed: olleh
It sounds simple enough, but depending on the language and constraints (like not using inbuilt functions), the implementation can vary.
You may think, why care about reversing a string when it looks so basic? Well, it teaches you a lot:
Plus, it is frequently asked in job interviews to test how you think and optimize code.
This is the most basic and widely used method, especially if you are just starting with programming. The idea is to loop through the string from the last character to the first and build a new string.
Copy Code
c
CopyEdit
#include <stdio.h>
#include <string.h>
int main() {
char str[100], rev[100];
int len, i, j;
printf("Enter a string: ");
fgets(str, sizeof(str), stdin);
str[strcspn(str, "\n")] = '\0'; // Remove newline
len = strlen(str);
j = 0;
for (i = len - 1; i >= 0; i--) {
rev[j++] = str[i];
}
rev[j] = '\0';
printf("Reversed string: %s\n", rev);
return 0;
}This is a clear and easy to understand approach, perfect for beginners.
This approach saves memory by not using another string. Instead, it reverses the string in the same memory space by swapping the characters at the start and end until they meet in the middle.
Copy Code
python
CopyEdit
def reverse_string(s):
s = list(s)
start = 0
end = len(s) - 1
while start < end:
s[start], s[end] = s[end], s[start]
start += 1
end -= 1
return ''.join(s)
print(reverse_string("uncodemy"))This is a common interview method, especially when they say do it without extra space.
If you want to test your understanding of recursive calls, try reversing a string recursively.
Copy Code
java
CopyEdit
public class ReverseString {
public static String reverse(String str) {
if (str == null || str.length() <= 1) {
return str;
}
return reverse(str.substring(1)) + str.charAt(0);
}
public static void main(String[] args) {
String input = "hello";
System.out.println("Reversed: " + reverse(input));
}
}This is great for understanding the call stack and is often used as a conceptual problem.
A stack follows the Last In First Out principle. So when you push each character into a stack and then pop them out, the string is reversed automatically.
Copy Code
cpp
CopyEdit
#include <iostream>
#include <stack>
using namespace std;
string reverseString(string str) {
stack<char> s;
for (char c : str) {
s.push(c);
}
string rev = "";
while (!s.empty()) {
rev += s.top();
s.pop();
}
return rev;
}
int main() {
string str = "developer";
cout << "Reversed: " << reverseString(str) << endl;
return 0;
}If you are allowed to use inbuilt features of the language, this becomes a one liner. But do not use this in interviews unless specifically allowed.
Copy Code
python
CopyEdit
print("Uncodemy"[::-1])Copy Code
javascript
CopyEdit
let str = "Uncodemy";
let rev = str.split("").reverse().join("");
console.log(rev);These shortcuts are great for scripting but are less preferred in formal tests because they do not show your understanding of logic.
In Java, StringBuilder and StringBuffer come with a built-in method called reverse() which makes reversal quick and thread safe.
Copy Code
java
CopyEdit
public class ReverseUsingBuilder {
public static void main(String[] args) {
StringBuilder sb = new StringBuilder("Uncodemy");
System.out.println("Reversed: " + sb.reverse().toString());
}
}Great for Java developers and especially useful when handling mutable strings in applications.
| Method | Memory Efficient | Easy to Understand | Best for Interviews |
| Iterative (Loop) | Moderate | Yes | Yes |
| Two Pointer Swap | Yes | Yes | Yes |
| Recursion | No (Stack usage) | Conceptual | Sometimes |
| Stack | No | Clear logic | Sometimes |
| Built-in Functions | Yes | Easiest | No (unless allowed) |
| StringBuilder / Buffer | Yes | Convenient | No |
Each method serves its own purpose depending on the situation. In competitive coding, you are expected to know all and choose wisely.
Reversing a string is not just about practice. It is used in:
So next time you reverse a string, know that you are touching a real world application logic.
These little details can make your code clean, efficient, and interview ready.
If this topic excites you and you want to go deeper into programming, Uncodemy offers one of the best Full Stack Development Courses. It covers:
Their course is well structured, beginner friendly, and completely focused on building a career in tech. You get mentorship, live doubt sessions, and assignments that include problems like reversing a string, palindrome checks, and more.
String reversal is a foundational topic that opens doors to understanding how programming works. It teaches you how to handle memory, loops, conditions, recursion, and data structures. The beauty of this problem lies in its simplicity and the variety of solutions it offers.
From using a basic loop to advanced recursion or built-in methods, you now have several tools under your belt. The next time you see a problem involving strings, you will know exactly how to approach it.
And remember, learning never stops. So go ahead, experiment with these methods, test their performance, and keep pushing your skills forward.
For a structured learning path and real world training, do not forget to check out Uncodemy’s Full Stack Development Course. It could be the stepping stone to your programming career.
Personalized learning paths with interactive materials and progress tracking for optimal learning experience.
Explore LMSCreate professional, ATS-optimized resumes tailored for tech roles with intelligent suggestions.
Build ResumeDetailed analysis of how your resume performs in Applicant Tracking Systems with actionable insights.
Check ResumeAI analyzes your code for efficiency, best practices, and bugs with instant feedback.
Try Code ReviewPractice coding in 20+ languages with our cloud-based compiler that works on any device.
Start Coding
TRENDING
BESTSELLER
BESTSELLER
TRENDING
HOT
BESTSELLER
HOT
BESTSELLER
BESTSELLER
HOT
POPULAR