Public Group

# char string reversal

This topic is 4449 days old which is more than the 365 day threshold we allow for new replies. Please post a new topic.

## Recommended Posts

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 :)

##### Share on other sites
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).

##### Share on other sites

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.

##### Share on other sites
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

1. 1
2. 2
3. 3
Rutin
22
4. 4
5. 5
khawk
14

• 9
• 11
• 11
• 23
• 12
• ### Forum Statistics

• Total Topics
633654
• Total Posts
3013168
×