Reversing a string

This is a program I have been stuck on now for several hours. The program I have to write needs to take a user input string containing characters and integers and output the reverse. The main function is supposed to ask the user for string, then call a function reverse with an algorithm to determine the reverse of the string, and then main prints the reversed array. Determining the reversed string I think is pretty easy, I can think of a couple of ways of doing it. The problem for me has been trying to determine how to define the string to use numbers and letters and pass that on to the reverse function. And then, of course, how to properly call the function back to main.

Here is the source code I have so far. It is kind of a mess, and may not be entirely consistent because I have been trying so many different approaches. Please ask if you need me to clarify anything.

where did it go?

Last edited on
Why don't you pass a string into the reverse function:
void reverse(string& str);

Or if you want to leave reverse(char*) you should invoke it like this:
reverse(str.c_str());

Your reverse should take a string, not a char*. There isn't a conversion from string to char*.

Meaningful variable names would help too. The algorithm looks reasonably sound, but you need to check the boundary conditions.
There is a standard reverse() function in the <algorithm> header such that.......
1
2
3
4
5
6
char s[] = "abcde fghij";
    cout<<"print string s \n";
    cout<<s;
    reverse(s,s+11);
    cout<<"\nprint reversed string s \n";
    cout<<s;

//prints jihgf edcba.
Topic archived. No new replies allowed.