Updated March 27, 2023
Introduction to Reverse String in C++
Reverse a string in C++ means reading or changing the order of reading characters in the reverse order or backward direction. The definition of a string is, the string is an order collection of characters or sequence of characters. A string is an object of std: string class in C++. On the string, we can perform any operation like a copy, concatenation, conversion, comparison, reverse, etc. For example, the string “hello” if we read in reverse order or backward direction is “olleh”, so the string “hello” reverse is “olleh”. To reverse a string we can write C ++ program and use it to perform the reverse operation on string. String in C++ we can store in a variable in two ways, one way is to use as an object of std: string class and the second way are to store in a character array.
The Logic for Reverse String
The given string first stored in the character array. The length of character array is stored in the variable j and variable i is initialized to 0 and with a for loop the string can be reversed iteratively inside it Using a temporary variable temp the ith character of character array is swapped with jth character. Once i become less than j the loop terminates. Hence we get the character array in reverse order.
Examples for the Reverse String
The examples of the following are given below:
Example #1
By using for loop
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string
void revStr(string& strg)
{
for (int i=strg.length()-1; i>=0; i--)
cout << strg[i];
}
// main program
int main()
{ string strg = "Hello world";
revStr(strg);
return 0;
}
Output:
As in the above output, we see the given string is printed in reverse order, but actually, it is not updated or edited to the string, just printing it.
Example #2
By using while loop
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string
void revStr(string& strg)
{
int i=strg.length();
while(i>=0)
{
cout <<strg[i];
i--;
}
}
// main program
int main()
{
string strg = "Hello world";
revStr(strg);
return 0;
}
Output:
As again an output we can see is printed in reverse order, again actually it is not updated or edited to the string.
Example #3
By using a do-while loop
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string
void revStr(string& strg)
{
int i=strg.length();
do
{
cout <<strg[i];
i--;
}while(i>=0);
}
// main program
int main()
{
string strg = "Hello world";
revStr(strg);
return 0;
}
Output:
As again an output we can see is printed in reverse order, again actually it is not updated or edited to the string.
Example #4
Next, we write the C++ code to understand Reverse a String more clearly with the following example where we apply the keydown ( ) method to the first input text box element –
By swapping characters:
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string
void revStr(string& strg)
{
int last = strg.length();
// characters are swaping from two both side
for (int i = 0; i < last / 2; i++)
// to swap characters using swap( ) built in function
swap(strg[i], strg[last - i - 1]);
}
// main code
int main()
{
string strg = "Hello world";
revStr(strg);
cout << strg;
return 0;
}
Output:
As in the above output, we see the given string is in reverse order.
Example #5
By using the reverse( ) built-in method
Code:
#include <bits/stdc++.h>
using namespace std;
int main()
{
string strg = "Hello world";
// using built in reverse method to reverse a string
// its syntax is void reverse(first, last)
reverse(strg.begin(), strg.end());
cout << strg;
return 0;
}
Output:
Example #6
By using const string
Code:
#include <bits/stdc++.h>
using namespace std;
// Function to reverse a string
char* revConstStr(char const* strg)
{
int len = strlen(strg);
// dynamic pointer create of type char array
char *ptr = new char[len+1];
// copy the string to ptr pointer
strcpy(ptr, strg);
// characters are swaping from two both side
for (int i=0, j=len-1; i<j; i++,j--)
swap(ptr[i], ptr[j]);
// return pointer of reversed string
return ptr;
}
// main code
int main(void)
{
const char *strg = "Hello world";
const char *str=revConstStr(strg);
for (int i=0; i<strlen(str); i++)
cout<<str[i];
return (0);
}
Output:
Conclusion
The string is an order collection of characters. To reverse a string in C++ is can be achieved by reading the string in reverse order character by character or backward direction character by character. A string is an object of std: string class in C++. We can reverse a string in multiple ways as in the above examples.
Recommended Articles
This is a guide to Reverse String in C++. Here we discuss the introduction, The Logic for Reverse String in C++ and the examples. You can also go through our other related articles to learn more –