char string reversal

Started by
2 comments, last by Nett 17 years, 6 months ago
hello & thanks for taking the time to read my post. I'm getting back into programming and figured I would do a little practice program with what memory serves me :D. Why does this loop require "--i" when the iteration back through the top loop should get the job done?

#include <iostream>

using namespace std;


int main(int argc, char *argv[])
{

	char name[20] = {0};

	name[0] = 'b';
	name[1] = 'e';
	name[2] = 'n';
	name[3] = '\0';
	
	char reverse_result[20] = {0};

	for(int i = count-1; i >= 0; i--)
	{
		for(int j = 0; j < count; j++)
		{
			reverse_result[j] = name;
			i--; // Wont work without this line, why?
		}
	} 


	return 0;
}

I don't understand how the variable I in the top for loop isn't the only one required. thanks for your time :)
Advertisement
I believe the outer for-loop shouldn't be there. Reversing a string is a linear operation, not quadratic. Instead, simply initialize i to count-1 (which I don't see defined anywhere, FWIW).
"Debugging is twice as hard as writing the code in the first place. Therefore, if you write the code as cleverly as possible, you are, by definition, not smart enough to debug it." — Brian W. Kernighan
0) We had a similar thread recently; please take a look.

1) Real text representation is provided by the standard library: it's called std::string, under <string>.

2) Reversing strings - actually, reversing the contents of any "sequential container", which could be a std::string, a std::vector, a std::list, a std::deque, or for that matter a char[] - is provided by the standard library; it's called std::reverse(), under <algorithm>.

3) As noted, 'count' is not initialized.

4) The reason is that the nested for loop isn't the code structure you want. Nesting the for loops means "For each position in the string going backwards, start a traversal going forwards again from the beginning".

It looks like this:

- 'i' is count-1.
- 'j' is 0.
- Inner loop: assign name - i.e. name[count-1] - to reverse_result[0].
- Inner loop: assign name - i.e. name[count-1] - to reverse_result[1].
- And so on, for the entire inner loop. Now reverse_result is full of copies of the last letter of name.
- Outer loop advances once, so 'i' is now count-2.
- Inner loop does its thing, overwriting all those copies of the last letter of the name with copies of the second-last letter of the name.

When you decrement 'i' within the inner loop, you cause the two traversals to happen simultaneously, which is what you want:

- 'i' is count-1.
- 'j' is 0.
- Inner loop: assign name[count-1] to reverse_result[0]. Decrement 'i', so now 'i' is count-2.
- 'j' is now 1, since we reached the end of the inner loop.
- Inner loop: assign name[count-2] to reverse_result[1].
- And so on. Eventually, every reverse_result gets assigned to name[count-1-i].
- Outer loop advances, but then finds that 'i' has already reached 0, because of the adjustments in the inner loop, and therefore bails out right away.

Conclusion: the outer loop is actually useless and should be discarded.
I appreciate the replies thanks you two, helped alot. Thats what I get for writing code at 3am and not sticking to trust-worthy std::string, just felt like brushing up a bit on arrays and failed :).

Thanks again - Nett

This topic is closed to new replies.

Advertisement