reversing string not working

This is a discussion on reversing string not working within the C++ Programming forums, part of the General Programming Boards category; Code: //function declaration string stringReverse(string arg) { string word = arg; for(int i = word.length(); i >= 0; i--) { ...

  1. #1
    village skeptic
    Join Date
    Aug 2008
    Posts
    31

    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?
    Last edited by misterMatt; 08-10-2008 at 04:36 PM.

  2. #2
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.

  3. #3
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    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];

  4. #4
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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.)

  5. #5
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    Code:
    swap
    0123
    
    pwas
    3120
    
    paws
    3210

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

  6. #6
    and the Hat of Guessing tabstop's Avatar
    Join Date
    Nov 2007
    Posts
    14,185
    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?

  7. #7
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    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?

  8. #8
    The Richness... Richie T's Avatar
    Join Date
    Jan 2006
    Location
    Ireland
    Posts
    469
    >> 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.
    No No's:
    fflush (stdin); gets (); void main ();


    Goodies:
    Example of fgets (); The FAQ, C/C++ Reference


    My Gear:
    OS - Windows XP
    IDE - MS Visual C++ 2008 Express Edition


    ASCII stupid question, get a stupid ANSI

  9. #9
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    or a MUCH easier way to do it:
    Code:
    string stringReverse( const string& arg )
    {
        string ret( arg.rbegin(), arg.rend() );
        return ret;
    }

  10. #10
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,635
    std::reverse is an algorithm, use it.

  11. #11
    village skeptic
    Join Date
    Aug 2008
    Posts
    31
    Quote Originally Posted by citizen View Post
    std::reverse is an algorithm, use it.
    That sort of defeats the purpose of the exercise though doesn't it?

  12. #12
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by misterMatt View Post
    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.

  13. #13
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    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).
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

  14. #14
    and the hat of sweating
    Join Date
    Aug 2007
    Location
    Toronto, ON
    Posts
    3,545
    Quote Originally Posted by laserlight View Post
    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.

  15. #15
    C++ Witch laserlight's Avatar
    Join Date
    Oct 2003
    Location
    Singapore
    Posts
    21,436
    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.
    C + C++ Compiler: MinGW port of GCC
    Version Control System: Bazaar

    Look up a C++ Reference and learn How To Ask Questions The Smart Way

Page 1 of 2 12 LastLast
Popular pages Recent additions subscribe to a feed

Similar Threads

  1. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 06:02 AM
  2. Something is wrong with this menu...
    By DarkViper in forum Windows Programming
    Replies: 2
    Last Post: 12-14-2002, 10:06 PM
  3. string array reversing...
    By cit in forum C Programming
    Replies: 4
    Last Post: 03-28-2002, 02:09 AM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21