# Help with strings

• 07-28-2008
69gto96z
Help with strings
I am working on a project, and need to display a string backwards. I have gotten it to where it will display the last letter in the string, "num" times, and "num" equals the length of the string. The problem I am having is it only shows the last number. Here is my instructions on this part.

"Using a loop and the square-bracket [] notation to access each individual character in the string, display the string backwards (last character in the string first, next to last, etc). Remember that the character positions in a string are numbered starting with 0, so the first position in the string would be stringname[0], while the last position in the string would be stringname[stringname.length()-1]. Also remember that individual positions in a string can be referenced using integer variables, such as an integer counter for a loop that goes from stringname.length()-1 down to 0."

This is what I have so far:
Code:

```counter = 0; do {   counter++;   cout << Char[Char.length()-1]; } while (counter < num);```
Any help would be appreciated, I just need to get pointed in the right direction and it should come fairly easy. Thanks!
• 07-28-2008
medievalelks
Sure, you're never changing which character gets printed. Always the last one, in this case.
• 07-28-2008
69gto96z
Okay, that makes sense? But am I on the right track? I cant for the life of me figure out how to change the character that is being printed.
• 07-28-2008
anon
How would you print forward? Not like this, I hope:
Code:

```do {   counter++;   cout << Char[0]; } while (counter < num);```
• 07-28-2008
medievalelks
Quote:

Originally Posted by 69gto96z
Okay, that makes sense? But am I on the right track? I cant for the life of me figure out how to change the character that is being printed.

It's specified by the index you put between [ and ]. Your code has the same index there for every trip through the loop. If Char is 10 characters in length, you're printing Char[9] every time. You need to vary what you put between the [ and ] as you go through the loop.
• 07-28-2008
69gto96z
Thanks for the replies! Got it all figured out :)