Reverse a String in C | C Program To Reverse a String

Reverse a String in C


his function is defined in the algorithm header file. The code given below describes the use of reverse() function,

#include <algorithm>

#include<iostream>

#include<string>

using namespace std;

int main()

{

    string str = "Journal Dev reverse example";

    reverse(str.begin(), str.end());

    cout<<"\n"<<str;

    return 0;

}

Output:

Use Of Reverse string in c++

Use Of reverse()

Using strrev()

strrev() is a pre-defined function in C++, defined inside the cstring.h header file. It is extensively applicable for reversing any C-string(character array).

Further, it only requires the base address of the string as its argument and reverses the string accordingly. Let us see how we can use the strrev() function in C++ to reverse strings.

#include<iostream>

#include<cstring>

using namespace std;

int main()

{

    char str[] ="Journal Dev reverse example";

    strrev(str);

    cout<<"\n"<<str;

    return 0;

}

Output: