# Thread: reversing string not working

1. ## reversing string not working[SOLVED, check source if you like]

Code:
```//function declaration
string stringReverse(string arg)
{
string word = arg;

for(int i = word.length(); i >= 0; i--)
{
char temp;

word[0] = temp;
word[0] = word[i];
word[i] = temp;

}

return word;

}```
If what I think I'm doing is correct, this should reverse a string, yes?

When I pass soemthing to the function, and then output the result - only blank spaces appear on the screen. Am I going out of bounds? What am I doing wrong here?

1. You don't just reverse the word -- you reverse it twice! Or rather you would, if the front moved forward (you shouldn't always be swapping with element 0).
2. If i = word.length(), then word[i] does not exist.

2. I'm having some difficulty wrapping my head around this. I can print (cout) it backwards easily... but for some reason this is really difficult for me.

Code:
```word[0] = temp;
//should be:
temp = word[0];```

3. Write it down on a piece of paper, for heaven's sake. What do you have to swap to reverse the word "swap"? (Hint: it's not 0&3, 0&2, 0&1, and 0&0 as you have it above.)

4. Code:
```swap
0123

pwas
3120

paws
3210```

I feel stupid that I still can't work it into an algorithm.

5. So you have 0<->3 and you have 1<->2. How do you get from 0 to 1? How do you get from 3 to 2?

6. Ok, I've figured it out. Finally. I made use of two different iterators instead of just the one like I tried to do - my output using the one was all palindromes.

Code:
```//function declaration
string stringReverse(string arg)
{
char temp;
string word = arg;
int length = word.length() - 1;

for(int i = 0, b = length; i <= b; i++, b--){
temp = word[i];
word[i] = word[b];
word[b] = temp;
}

return word;

}```
I'm new to the whole coding thing, so it's a little difficult for me to wrap my head around simple stuff like this. The idea is that I keep switching until the two counters meet or pass each other. is there any way I could improve upon this implementation?

I feel a little better now that I figured it out, but I still feel a little bit like a buffoon. Did you guys have similar troubles when you started out?

7. >> is there any way I could improve upon this implementation?

Yes, try getting it working with a single iterating variable. It can be done. As a hint, try printing out the values of i, b and length for each iteration of the loop and see if you can observe a relationship.

>> I feel a little better now that I figured it out, but I still feel a little bit like a buffoon. Did you guys have similar troubles when you started out?

Most definitely. Don't become discouraged - you are only beginning, and it can take a while to get into a programming mindset. I recall when I was starting out that I had an assignment which involved using a while loop to complete some task, and there were a number of conditions which were supposed to cause different things to be executed (call them conditions A, B and C for convenience, implemented with if statements, and that they excluded the condition for the loop). I kept thinking that while none of these conditions were true that there should be a do nothing statement until the next iteration of the loop! I felt that I had to explicitly take account of this case.

I also had trouble with relatively simple concepts like scope, and I recall one occassion where I left braces out of an if statement but had indented a few lines of code and I couldn't understand why the extra few lines where always being executed regardless of the condition for the if statement! I look back at these mistakes and cringe, but I believe that everyone has comparable stories from when they started out.

8. or a MUCH easier way to do it:
Code:
```string stringReverse( const string& arg )
{
string ret( arg.rbegin(), arg.rend() );
return ret;
}```

9. std::reverse is an algorithm, use it.

10. Originally Posted by citizen
std::reverse is an algorithm, use it.
That sort of defeats the purpose of the exercise though doesn't it?

11. Originally Posted by misterMatt
That sort of defeats the purpose of the exercise though doesn't it?
If it's for school and they let you use the STL string class, then they should be teaching you the right way of doing it, rather than teaching you how to reinvent the wheel.

12. If it's for school and they let you use the STL string class, then they should be teaching you the right way of doing it, rather than teaching you how to reinvent the wheel.
If it is school, then they should both teach you how to reinvent the wheel (so you can invent greater things) and how to use it (so you can effectively use the wheel to invent greater things).

13. Originally Posted by laserlight
If it is school, then they should both teach you how to reinvent the wheel (so you can invent greater things) and how to use it (so you can effectively use the wheel to invent greater things).
If they're teaching you how to do it manually, they should require you to use a char array. Teaching them to do it manually with a string class is just teaching them bad habits, and creating C+ programmers instead of C++ programmers.

14. If they're teaching you how to do it manually, they should require you to use a char array. Teaching them to do it manually with a string class is just teaching them bad habits, and creating C+ programmers instead of C++ programmers.
That is absurd: you are saying that one must first teach the C portion of C++ in order to teach basic concepts. This would render C++ almost useless as a medium of instruction, since one might as well use C and ignore C++. Remember, the aim here is not only to learn C++, but to learn how to solve problems and acquire programming skills that can be applied beyond the class room. The ability to recognise when a library function may be applied is certainly part of that, but the ability to implement library functions is also an important part of that learning.

Popular pages Recent additions