0
C++ How to reverse a name.
I can't seem to figure out how I would reverse a name someone inputs. I know that i can use a for loop and maybe use strings to but I don't know in what order and stuff. could anyone help out.
4 Antworten
+ 3
#include <iostream>
#include <string>
#include <algorithm> // to get the reverse function
using namespace std;
int main()
{
string mystring = "hello world";
reverse(mystring.begin(), mystring.end());
cout << mystring;
return 0;
}
+ 1
string mystring = "just some rubbish";
string rev_mystring = "";
for(int x = mystring.length()-1; x > -1; x--)
rev_mystring+=mystring[x];
cout << rev_mystring;
0
But is there a way without using the reverse function? Only using string, and For loop.
0
Thank you