#1
  1. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep Power
    0

    Need help reversing a string


    My problem is that when I run this program my output is not right, it should reverse the string, but instead it prints out random characters. Any help would be appreciated




    PHP Code:
    #include <iostream.h>

    char string[11];

    void reverse()
    {
        
    int character 9;
        
    int character_two 0;
        
    char string_two[11];
        while(
    character != -1)
        {        
            
    string[character] = string_two[character_two];
            
    character--;
            
    character_two++;
        }
        
    cout << string << endl;
    }
         
    main()
    {
        
    char string[11];
        
    cout << "Enter a string of characters to reverse, no longer than 10 characters" << endl;
        
    cin.get(string11);
        
    cin.ignore(50'\n');
        
    reverse();
        return 
    0;

  2. #2
  3. *bounce*
    Devshed Novice (500 - 999 posts)

    Join Date
    Jan 2002
    Location
    Delft, The Netherlands
    Posts
    514
    Rep Power
    42
    Ok, since you allocated 11 bytes and print out a message asking for no more than 10 chars, you know that strings in C are NUL-terminated. So far, so good :)

    But in reverse(), you assume that the user will enter exactly 10 chars. If I enter "hey", you'll be copying way past the end of the string, since you start at 9.

    What you need to do, is look for the terminating NUL-byte, and then work your way back. Below is some code I slapped together for the occasion; don't look at it if you want to work it out yourself first.

    Code:
    void reverse (char *s)
    {
            int i, j;
            char c;
    
            for (i = 0; s[i]; i++)
                    ;       /* do nothing */
    
            i--; /* step back from the NUL-byte */
    
            for (j = 0; j < i; i--, j++) {
                    c = s[j];
                    s[j] = s[i];
                    s[i] = c;
            }
    }
    I hope this makes sense to you. :)
    "A poor programmer is he who blames his tools."
    http://analyser.oli.tudelft.nl/
  4. #3
  5. Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jun 2000
    Location
    Southern California
    Posts
    73
    Rep Power
    15
    What you need to do, is look for the terminating NUL-byte, and then work your way back
    Here's another example:

    Code:
    void reverse(char* s)
    {
        char temp, *end;
    
        // point to last non-null char
        end = s + strlen(s) - 1;
    
        while (s < end) {
            temp   = *s;
            *s++   = *end;
            *end-- = temp;
        }
    }
    OR, in C++:

    Code:
    #include <string>
    using namespace std;
    
    int main(int argc, char** argv)
    {
        string s = "raboof";
        string::reverse_iterator ri;
        string s2;
    
        for (ri = s.rbegin(); ri != s.rend(); ri++)
            s2 += *(ri.base()-1);
    
        cout << s2 << endl;
        return 0;
    }
  6. #4
  7. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep Power
    0
    thanks to both of you
  8. #5
  9. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Aug 2002
    Posts
    272
    Rep Power
    19
    Another issue is that you input the string into one instance of "string" and then you reverse another instance that is uninitialized.
  10. #6
  11. No Profile Picture
    Offensive Member
    Devshed Novice (500 - 999 posts)

    Join Date
    Oct 2002
    Location
    in the perfect world
    Posts
    622
    Rep Power
    27
    Unless we are doing homework there is

    _strrev()

    from string.h
    The essence of Christianity is told us in the Garden of Eden history. The fruit that was forbidden was on the Tree of Knowledge. The subtext is, All the suffering you have is because you wanted to find out what was going on. You could be in the Garden of Eden if you had just kept your f***ing mouth shut and hadn't asked any questions.

    Frank Zappa
  12. #7
  13. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2003
    Posts
    3
    Rep Power
    0
    If you're not doing homework, you might as well save yourself a headache and use std::string and std::reverse (found in <algorithm>).
  14. #8
  15. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep Power
    0
    hm...looking back over my problem I fixed this quite easially...
    I changed:

    PHP Code:
    string[character] = string_two[character_two]; 
    into:
    PHP Code:
    string_two[character_two] = string[character]; 
  16. #9
  17. No Profile Picture
    Junior Member
    Devshed Newbie (0 - 499 posts)

    Join Date
    Jan 2003
    Posts
    4
    Rep Power
    0
    I also needed to change the while loop, and added

    PHP Code:
    || string[character] != '\0'
    edit: should be != '\0' , but it didn't show up.

IMN logo majestic logo threadwatch logo seochat tools logo