Get free ebooK with 50 must do coding Question for Product Based Companies solved
Fill the details & get ebook over email
Thank You!
We have sent the Ebook on 50 Must Do Coding Questions for Product Based Companies Solved over your email. All the best!

CPP Program To Print the Given String in Reverse Order

Last Updated on May 11, 2023 by Prepbytes

The built-in reverse() method, the string’s constructor, or your own function that reverses a string by swapping its elements can all be used in C++ programming when you need to reverse a string.

Additionally, you can print a string in reverse by looping over it and printing it if you just want to print strings in reverse.

In C++, there are many situations in which you might want to reverse a string, whether it be to print it backward or to just reverse the text for use in other operations like palindrome detection.

Approaches used to Print the Given String in Reverse Order

In any case, we’ll look at three distinct approaches to creating a reversed string in C++:

  • Using the predefined reverse() function
  • Using the string constructor
  • Creating your own function to reverse a string

Let’s examine the C++ process for reversing a string.

Using reverse() Function in C++

The C++ language comes with a built-in function called reverse(). You must import the algorithm header file in order to utilize this method.

The bidirectional begin and bidirectional end iterators to the string are the two parameters that are passed to the algorithm header file’s reverse() function. In essence, a bidirectional iterator is a forward iterator with the added ability to support the prefix and postfix decrement operators.

We utilize bidirectional iterators to access elements in both directions, that is, towards the beginning and towards the finish. List, set, map, and other containers are a few of the C++ types that support these bidirectional iterators.

Iterators (things that can be iterated or looped over) are returned when functions like begin(), end(), and others are used on a string.

Code Implementation

#include <algorithm>
#include <iostream>
#include <string>

using namespace std;

int main(){
    string word = "prepbytes";
    // passing the begin() and end() iterators of the string
    reverse(word.begin(), word.end());
    cout << word;
    return 0;
}

Output

setybperp

Using Constructor of String

The next widely used technique for reversing a string is to pass the reverse iterator to the constructor of the string. We’ll use the rbegin() and rend() functions to pass this reverse iterator. Observe how the previous method’s straightforward begin() and end() iterators were employed.

Code Implementation

// include the header file
#include <bits/stdc++.h>

using namespace std;

int main(){
    string word = "Prepbytes";
    // Pass reverse iterators
    string reversed = string(word.rbegin(), word.rend());
    // print the reversed string
    cout << reversed << endl;
    return 0;
}

Output

setybperP

By employing this technique, we are merely producing a new string that is produced by the rbegin() and rend() iterators of the original string rather than altering it.

Making Our String Reversing Function

We’ve already seen 2 methods for reversing strings; now it’s time to build our own function.

If you carefully examine the original string and the inverted string, as in this:

Original: Prepbytes
Reversed: setybperP

As you can see, the reversed string merely has the original string’s letters switched around. How?

Simply swap the first letter of the word P for the final letter of the word s. Similar swapping occurs between the second letter r and the second-to-last letter e, and so on.

Therefore, we can simply swap the first half of the word’s letters with the second half of the word if we want to design our own function. In this method, we’ll alter the original string directly to stop it from evolving into another string.

Here is how we would implement it in code: since we only need to swap the first half of the string with the second half, we would only traverse half of the string. By reference, this exchange is carried out.

Code Implementation

#include<iostream>
#include<string>

using namespace std;

string reverse_string(string word){
    int length = word.length();

    // Swap elements starting from two corners
    for (int i = 0; i < length / 2; i++)
        swap(word[i], word[length - i - 1]);

    return word;
}

int main(){
    string word = "PrepBytes";
    cout << reverse_string(word) << endl;
    return 0;
}

Output :

setyBperP

Printing a String in Reverse

Now that we understand how to reverse a string, we can easily print the string backward by looping through the string backward and writing each element.

Code Implementation

#include<iostream>
#include<string>

using namespace std;

void print_reverse(string word){
    int length = word.length();
    for (int i = length-1; i >= 0; i--){
        cout << word[i];
    }
}

int main(){
    string word = "PrepBytes";
    print_reverse(word);
    return 0;
}

Output

setyBperP

Conclusion

  • In C++, there are 3 ways to reverse a string:
    • Using the reverse() function
    • Using a constructor.
    • Make your element-swapping function.
  • By iterating over a string starting at the opposite end, you may also print it backward.

FAQ Related to CPP Program To Print The Given String in Reverse Order

Q1. Are all character array strings?
Ans. No. The string is used to represent and store character sequences as a single entity and as a single data type. On the other hand, a character array is a sequential collection of char data, where each entry is a distinct object.

Q2. Is a string always an array?
Ans. A one-dimensional array of characters with the null character at the end makes up the string. The array of strings will transform into a two-dimensional array of characters since strings are arrays of characters.

Q3. What type of array is a string?
Ans. A definite number of String values make up a String Array. An ordered string of characters. In general, a string is an immutable object, meaning its value cannot be altered. Similar to other Array data types, the String Array operates similarly.

Leave a Reply

Your email address will not be published. Required fields are marked *