Thread: Help with strings

  1. #1
    Registered User
    Join Date
    Jun 2008
    Posts
    6

    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!

  2. #2
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Sure, you're never changing which character gets printed. Always the last one, in this case.

  3. #3
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    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.

  4. #4
    The larch
    Join Date
    May 2006
    Posts
    3,573
    How would you print forward? Not like this, I hope:
    Code:
    do {
      counter++;
      cout << Char[0];
    } while (counter < num);
    I might be wrong.

    Thank you, anon. You sure know how to recognize different types of trees from quite a long way away.
    Quoted more than 1000 times (I hope).

  5. #5
    Registered User
    Join Date
    Apr 2008
    Posts
    890
    Quote Originally Posted by 69gto96z View Post
    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.
    Last edited by medievalelks; 07-28-2008 at 11:45 AM.

  6. #6
    Registered User
    Join Date
    Jun 2008
    Posts
    6
    Thanks for the replies! Got it all figured out

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Strings Program
    By limergal in forum C++ Programming
    Replies: 4
    Last Post: 12-02-2006, 03:24 PM
  2. Programming using strings
    By jlu0418 in forum C++ Programming
    Replies: 5
    Last Post: 11-26-2006, 08:07 PM
  3. Reading strings input by the user...
    By Cmuppet in forum C Programming
    Replies: 13
    Last Post: 07-21-2004, 06:37 AM
  4. damn strings
    By jmzl666 in forum C Programming
    Replies: 10
    Last Post: 06-24-2002, 02:09 AM
  5. menus and strings
    By garycastillo in forum C Programming
    Replies: 3
    Last Post: 04-29-2002, 11:23 AM