As a programmer, you've likely faced a situation that requires you to reverse a string. Reversing a string is one of the most common situations programmers face while learning to code. You can reverse a string by using built-in functions or by writing your own implementation of the reverse function.
In this article, you'll learn about different methods to reverse a string in C++, Python, and JavaScript.
Different Methods to Reverse a String in C++
You can reverse a string in C++ using these methods:
Reverse a String in C++ Using the Built-in reverse() Function
Below is the C++ program to reverse a string using the built-in reverse() function:
// C++ implementation to reverse a string
// using inbuilt function: reverse()
#include <bits/stdc++.h>
using namespace std;
// Driver Code
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";
cout << "Input string:" << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
reverse(str3.begin(), str3.end());
cout << "Reversed string: " << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in C++ by Swapping Characters
Below is the C++ program to reverse a string by swapping characters:
// C++ implementation to reverse a string
// by swapping characters
#include <bits/stdc++.h>
using namespace std;
// Own implementation of a function to reverse a string
void reverseString(string& str)
{
int size = str.size();
for(int i=0, j=size-1; i<size/2; i++, j--)
{
swap(str[i], str[j]);
}
}
// Driver Code
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";
cout << "Input string:" << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
reverseString(str1);
reverseString(str2);
reverseString(str3);
cout << "Reversed string: " << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in C++ Using Reverse Iterators With a Constructor
Below is the C++ program to reverse a string using reverse iterators with a constructor:
// C++ implementation to reverse a string
// using constructor
#include <bits/stdc++.h>
using namespace std;
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";
cout << "Input string:" << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
// Using reverse iterators to reverse a string
string reversedStr1 = string(str1.rbegin(), str1.rend());
string reversedStr2 = string(str2.rbegin(), str2.rend());
string reversedStr3 = string(str3.rbegin(), str3.rend());
cout << "Reversed string: " << endl;
cout << reversedStr1 << endl;
cout << reversedStr2 << endl;
cout << reversedStr3 << endl;
return 0;
}
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in C++ Using a Temporary String
Below is the C++ program to reverse a string using a temporary string:
// C++ implementation to reverse a string
// using a temporary string
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string using a temporary string
string reverseString(string str)
{
int size = str.size();
string tempStr;
for(int i=size-1; i>=0; i--)
{
tempStr.push_back(str[i]);
}
return tempStr;
}
// Driver Code
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";
cout << "Input string:" << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
cout << "Reversed string: " << endl;
cout << str1 << endl;
cout << str2 << endl;
cout << str3 << endl;
return 0;
}
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Different Methods to Reverse a String in Python
You can reverse a string in Python using these methods:
Reverse a String in Python Using Extended Slice Syntax
Below is the Python program to reverse a string using an extended slice syntax:
# Python implementation to reverse a string
# using extended slice syntax
def reverseString(str):
return str[::-1]
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
print("Input string:")
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print("Reversed string:")
print(str1)
print(str2)
print(str3)
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in Python Using Recursion
Below is the Python program to reverse a string using recursion:
# Python implementation to reverse a string
# using recursion
def reverseString(str):
if len(str) == 0:
return str
else:
return reverseString(str[1:]) + str[0]
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
print("Input string:")
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print("Reversed string:")
print(str1)
print(str2)
print(str3)
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in Python Using the Built-in reversed() Method
Below is the Python program to reverse a string using the built-in reversed() method:
# Python implementation to reverse a string
# using reversed method()
def reverseString(str):
str = "".join(reversed(str))
return str
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
print("Input string:")
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print("Reversed string:")
print(str1)
print(str2)
print(str3)
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in Python Using a Temporary String
Below is the Python program to reverse a string using a temporary string:
# Python implementation to reverse a string
# using a temporary string
def reverseString(str):
tempStr = ""
for s in str:
tempStr = s + tempStr
return tempStr
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
print("Input string:")
print(str1)
print(str2)
print(str3)
str1 = reverseString(str1)
str2 = reverseString(str2)
str3 = reverseString(str3)
print("Reversed string:")
print(str1)
print(str2)
print(str3)
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Different Methods to Reverse a String in JavaScript
You can reverse a string in JavaScript using these methods:
Reverse a String in JavaScript Using Recursion
Below is the JavaScript program to reverse a string using recursion:
// JavScript implementation to reverse a string
// using recursion
function reverseString(str) {
if (str === "") {
return "";
} else {
return reverseString(str.substr(1)) + str.charAt(0);
}
}
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
document.write("Input string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write("Reversed string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in JavaScript Using Built-in Methods
Below is the JavaScript program to reverse a string using built-in methods:
// JavaScript implementation to reverse a string
// using inbuilt methods
function reverseString(str) {
return str.split("").reverse().join("");
}
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
document.write("Input string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write("Reversed string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Reverse a String in JavaScript Using a Temporary String
Below is the JavaScript program to reverse a string using a temporary string:
// JavScript implementation to reverse a string
// using a temporary string
function reverseString(str) {
var size = str.length;
tempStr = "";
for(let i=size-1; i>=0; i--)
{
tempStr += str[i];
}
return tempStr;
}
str1 = "MUO";
str2 = "Welcome to MUO";
str3 = "She sells seashells by the seashore";
document.write("Input string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
document.write("Reversed string:
");
document.write(str1 + "
");
document.write(str2 + "
");
document.write(str3 + "
");
Output:
Input string:
MUO
Welcome to MUO
She sells seashells by the seashore
Reversed string:
OUM
OUM ot emocleW
erohsaes eht yb sllehsaes slles ehS
Learn String Manipulation
For solving string-related interview problems, you must know how to manipulate a string. You can manipulate a string in any programming language like C++, Python, JavaScript, Java, C, etc.
Python provides the most easy-to-understand syntax to manipulate a string. If manipulating string seems difficult to you, give Python a go; it's deceptively straightforward.