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 *