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.
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());
}
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;
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