Given a string, write a C/C++ program to reverse it.

- Write own reverse function by swapping characters: One simple solution is to write our own reverse function to reverse a string in C++.
// A Simple C++ program to reverse a string#include <bits/stdc++.h>usingnamespacestd;// Function to reverse a stringvoidreverseStr(string& str){intn = str.length();// Swap character starting from two// cornersfor(inti = 0; i < n / 2; i++)swap(str[i], str[n - i - 1]);}// Driver programintmain(){string str ="geeksforgeeks";reverseStr(str);cout << str;return0;}chevron_rightfilter_noneOutput :
skeegrofskeeg
- Using inbuilt “reverse” function: There is a direct function in “algorithm” header file for doing reverse that saves our time when programming.
// Reverses elements in [begin, end] void reverse (BidirectionalIterator begin, BidirectionalIterator end);
// A quickly written program for reversing a string// using reverse()#include <bits/stdc++.h>usingnamespacestd;intmain(){string str ="geeksforgeeks";// Reverse str[begin..end]reverse(str.begin(), str.end());cout << str;return0;}chevron_rightfilter_noneOutput :
skeegrofskeeg
- Only printing reverse:
// C++ program to print reverse of a string#include <bits/stdc++.h>usingnamespacestd;// Function to reverse a stringvoidreverse(string str){for(inti=str.length()-1; i>=0; i--)cout << str[i];}// Driver codeintmain(void){string s ="GeeksforGeeks";reverse(s);return(0);}chevron_rightfilter_noneOutput:
skeegrofskeeG
- Getting reverse of a const string:
// C++ program to get reverse of a const string#include <bits/stdc++.h>usingnamespacestd;// Function to reverse string and return// reverse string pointer of thatchar* reverseConstString(charconst* str){// find length of stringintn =strlen(str);// create a dynamic pointer char arraychar*rev =newchar[n+1];// copy of string to ptr arraystrcpy(rev, str);// Swap character starting from two// cornersfor(inti=0, j=n-1; i<j; i++,j--)swap(rev[i], rev[j]);// return pointer of the reversed stringreturnrev;}// Driver codeintmain(void){constchar*s ="GeeksforGeeks";printf("%s", reverseConstString(s));return(0);}chevron_rightfilter_noneOutput:
skeeGrofskeeG
- Reverse string using the constructor : Passing reverse iterators to the constructor returns us a reversed string.
// A simple C++ program to reverse string using constructor#include <bits/stdc++.h>usingnamespacestd;intmain(){string str ="GeeksforGeeks";//Use of reverse iteratorsstring rev = string(str.rbegin(),str.rend());cout<<rev<<endl;return0;}chevron_rightfilter_noneOutput:
skeeGrofskeeG
This article is contributed by Priyam kakati, Ranju Kumari, Somesh Awasthi and improved by Supratik Mitra, Lakshay Bansal. If you like GeeksforGeeks and would like to contribute, you can also write an article and mail your article to contribute@geeksforgeeks.org. See your article appearing on the GeeksforGeeks main page and help other Geeks.
Please write comments if you find anything incorrect, or you want to share more information about the topic discussed above
Attention reader! Don’t stop learning now. Get hold of all the important DSA concepts with the DSA Self Paced Course at a student-friendly price and become industry ready.
Recommended Posts:
- Taking String input with space in C (3 Different Methods)
- 5 Different methods to find length of a string in C++
- Reverse string in Python (5 different ways)
- Different methods to copy in C++ STL | std::copy(), copy_n(), copy_if(), copy_backward()
- Program to count digits in an integer (4 Different Methods)
- Create a new string by alternately combining the characters of two halves of the string in reverse
- Check if a given string is a Reverse Bitonic String or not
- Minimum cuts required to convert a palindromic string to a different palindromic string
- Python String Methods | Set 2 (len, count, center, ljust, rjust, isalpha, isalnum, isspace & join)
- Print all distinct characters of a string in order (3 Methods)
- Methods to concatenate string in C/C++ with Examples
- Count consonants in a string (Iterative and recursive methods)
- Python String Methods | Set 1 (find, rfind, startwith, endwith, islower, isupper, lower, upper, swapcase & title)
- Print reverse of a string using recursion
- Write a program to reverse an array or string
- Reverse a string without affecting special characters
- Reverse string without using any temporary variable
- Reverse vowels in a given string
- Reverse words in a given String in Java
- Print words of a string in reverse order

