How to Reverse a String in C++, Python, and JavaScript

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
using namespace std;
// Driver Code
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";
cout cout cout cout reverse(str1.begin(), str1.end());
reverse(str2.begin(), str2.end());
reverse(str3.begin(), str3.end());
cout cout cout cout 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
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 {
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 cout cout cout reverseString(str1);
reverseString(str2);
reverseString(str3);
cout cout cout cout 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
using namespace std;
int main()
{
string str1 = "MUO";
string str2 = "Welcome to MUO";
string str3 = "She sells seashells by the seashore";

cout cout cout cout // 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 cout cout cout 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
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 cout cout cout str1 = reverseString(str1);
str2 = reverseString(str2);
str3 = reverseString(str3);
cout cout cout cout
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

Related: How to Find Vowels, Consonants, Digits, and Special Characters in a String

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:

Related: What Is Recursion and How Do You Use It?

# 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:

Related: How to Create Your First React App With JavaScript

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.

Source: makeuseof.com

Related posts

The 5 Biggest Features and Changes in Apple’s OLED iPad Pros

Switch 2: 5 Things Id Like to See More Than Just Power

Switch 2: 5 Things Id Like to See More Than Just Power