Reverse of an input string

hey, I need help with the reversing a string. so if any could explain this to me

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
 /* Reversing a string */
#include <iostream>
#include <string>
using namespace std;
void reverseString(string in_str);
int main() {
     string str1 = "Hello World!";
     string str2 = "racecar";
     string str3 = "a man a plan a canal-panama";
     string str4 = " ";
     reverseString(str1);
     cout << endl;
     reverseString(str2);
     cout << endl;
     reverseString(str3);
     cout << endl;
     reverseString(str4);
return 0; }
void reverseString(string in_str)
{
     // Your code goes here
}
Last edited on
A hint: http://www.cplusplus.com/reference/utility/swap/

Edit: scratch that; your function does not return anything.

Different hint: Look at the characters, starting from last and stopping after first.
Last edited on
Say you have indices i, at the start of the string and j, at the end of the string.
Swap the characters at i and j.
Increment i and decrement j.
Repeat this until i meets j.

For example,
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
"Hello World!"

i = 0, j = 11
'H'  , '!'
> "!ello WorldH"

i = 1, j = 10
'e'  , 'd'
> "!dllo WorleH"

i = 2, j = 9
'l'  , 'l'
> "!dllo WorleH"

i = 3, j = 8
'l'  , 'r'
> "!dlro WolleH"

...
I need help with the reversing a string
There is a std::string ctor that takes 2 input iterators and copies the sequence of characters b/w the two iterators in

the same order, so you can pass rbegin() and rend() of the original std::string to this ctor to make a reversed

std::string in one step:
1
2
3
4
5
6
7
8
#include <iostream>
#include <string>

int main()
{
    std::string original = "my string";
    std::string reversed(original.rbegin(), original.rend());
}

the function should not return anything.
i have to reverse it manually by count the length but I don't know how
the function should not return anything.
i have to reverse it manually


These were not mentioned in the OP, in fact the post header reads:
Reverse of an input string automatically
Last edited on
sorry I fixed it
Your reverseString(string in_str) can have a length member function to find the length of the string. Then use a loop to iterate the string backwards and store it in another string.

1
2
3
4
5
6
7
8
// Pusdo code example
aString = "Hello World";
reverse(aString);
> Length = 11
> string reversedString;
> loop should be reading d first
> reversedString += aString.at(index)
> cout << reveresedString;
Last edited on
the function should not return anything.
1
2
3
4
5
int main() {
     string str1 = "Hello World!";
     reverseString(str1);
    // Now what?  We got nothing and str1 is still "Hello World!"
}

In other words the function does nothing (as far as the caller is concerned).

It seems likely that the function should print something.
Print the input argument in reverse?

Printing a string in reverse is almost, but not quite, the same as reversing the string.
Reversing a string is almost, but not quite, the same as creating a copy that has content reversed.

Sure, the function can do all the reversal in the world, but as long as the user of the function does not see the result in any way we have only your word that that the function does what it should.

If that seems pedantic, then do remember that being pedantic is a virtue that saves you from hassle in programming.
i have to reverse it manually by count the length but I don't know how

In this case the "How long is a piece of string?" has exact answer:
http://www.cplusplus.com/reference/string/string/size/
Topic archived. No new replies allowed.