C++ program to reverse given string.

Sample Input:

Hello World!
Lorem ipsum dolor sit amet, consectetur adipiscing elit, sed do eiusmod tempor incididunt ut labore et dolore magna aliqua.

!dlroW olleH
.auqila angam erolod te erobal tu tnudidicni ropmet domsuie od des ,tile gnicsipida rutetcesnoc ,tema tis rolod muspi meroL


// ? 2017 TheFlyingKeyboard and released under MIT License
// theflyingkeyboard.net 

#include <iostream>
#include <string>

using namespace std;

string reverseString(string stringToReverse);

int main(){
  string stringToReverse;
  cout << "Enter a string you want to reverse" << endl;
  getline(cin, stringToReverse);
  cout << reverseString(stringToReverse) << endl;
  return 0;

string reverseString(string stringToReverse){
  char tmp;
  for(int i = 0; i < stringToReverse.size() / 2; i++){
    tmp = stringToReverse[stringToReverse.size() - 1 - i];
    stringToReverse[stringToReverse.size() - 1 - i] = stringToReverse[i];
    stringToReverse[i] = tmp;
  return stringToReverse;


C++ Reverse A String
Tagged on:     

Leave a Reply

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

By continuing to use the site, you agree to the use of cookies. You can read more about it the Cookies&Privacy Policy Section Above. more information

The cookie settings on this website are set to "allow cookies" to give you the best browsing experience possible. If you continue to use this website without changing your cookie settings or you click "Accept" below then you are consenting to this. You can read more about it the Cookies&Privacy Policy Section.