Reversing a string
Nov 19, 2014 at 8:34am UTC
I am having trouble reversing the string so that I can input the name in the middle of the reversed string. Where is my function wrong?
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41
#include<iostream>
#include<iomanip>
#include<cmath>
#include<string>
#include<fstream>
using namespace std;
string random;
void reverse();
int main()
{
string name;
cout<<"Please input your name and a random string of characters" <<endl;
cout<<"hit enter after your name" <<endl;
getline(cin, name);
getline(cin, random);
reverse();
random.insert(int (random.length())/2, name);
cout<<random;
system("pause" );
return 0;
}
void reverse()
{
int i;
int j = (random.length()-1);
for (i = j; i>=0; i--)
{
random[i-j] = random[j];
}
return ;
}
Nov 19, 2014 at 11:06am UTC
Can you tell me what the values for i and j (i-j would be helpful for you as well) are for each iteration of the loop on line 35? This will help you understand why it isn't working.
Topic archived. No new replies allowed.