Skip to content

Instantly share code, notes, and snippets.

@gwang
Created August 18, 2015 07:16
Show Gist options
  • Save gwang/6d947057e527b33be3e3 to your computer and use it in GitHub Desktop.
Save gwang/6d947057e527b33be3e3 to your computer and use it in GitHub Desktop.
reverse a string in C++
#include <string>
#include <iostream>
using namespace std;
void reverse(string s)
{
cout << "input = " << s << endl;
cout << "size = " << s.length() << endl;
for (int i = s.length()-1; i>=0; i--) cout << s[i];
cout << endl;
}
int main(int argc, char **argv)
{
string s;
cout << "Please enter the string to be reversed:" << endl;
getline(cin, s);
reverse(s);
// or we can just pass the string from the command line:
// cout << argv[1] << endl;
// reverse(argv[1]);
return (0);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment