reverse a string

in a situation like the one below, how would you go about reversing the string?

1
2
3
4
5
6
7
8
9
10
11
12
13
14
#include<iostream>
#include <string>

using namespace std;

int main () {
string word;

cin>>word;

//reverse word and save it as string revword

cout<<word
}



this isnt by any means the actual code i am using this function for, but it gets the point across of the question im asking. How would you go about reversing an inputted string and reversing it like so?
thanks for the response. after looking it over this is what ive got, and it seems to work great! =]

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21

#include <iostream>


using namespace std;

int main () {
 string word;
string::iterator it;
cin>>word;
int a;
string revword;
  reverse(word.begin(),word.end());
  for (it=word.begin(); it!=word.end(); ++it)
revword+=*it;
  cout << revword;

  return 0;
}

Unnecessary variables...

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>

int main()
{
    std::string word; // string variable
    
    std::cout << "Please enter a word: ";
    getline(std::cin, word); // get input
    
    std::cout << "\nOriginal word: ";
    std::cout << word << "\n\n";
    
    reverse(word.begin(), word.end());
    
    std::cout << "Reversed word: ";
    std::cout << word;
    
    std::cin.get();
    return 0;
}
Topic archived. No new replies allowed.