Thread: word reverser

  1. #16
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by kermit
    But does it do this:

    my name is Jim

    mIJ si eman ym

    or this:

    Jim is name my

    ??
    I agree it does not reverse words, but I fail to understand why it does not reverse a string.
    The one who says it cannot be done should never interrupt the one who is doing it.

  2. #17
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >but I fail to understand why it does not reverse a string
    In quzah's program length is only extended by the length of bar, not the length of bar plus any whitespace walked over. When foo + length is used to call word again, the string is off by a few characters.
    My best code is written with the delete key.

  3. #18
    Registered User pinko_liberal's Avatar
    Join Date
    Oct 2001
    Posts
    284
    Quote Originally Posted by Prelude
    >but I fail to understand why it does not reverse a string
    In quzah's program length is only extended by the length of bar, not the length of bar plus any whitespace walked over. When foo + length is used to call word again, the string is off by a few characters.
    I am sorry, I am a bit slow today....I did not get your point at all. let a b c d ... denote arbitary characters.
    Is there any string say : abcd...x'\0' such that quzahs program will not print
    x....cba to the screen
    Irrespective of what a b c d are?
    The one who says it cannot be done should never interrupt the one who is doing it.

  4. #19
    Code Goddess Prelude's Avatar
    Join Date
    Sep 2001
    Posts
    9,897
    >Irrespective of what a b c d are?
    No, quzah's revword works properly. However, coupled with the word function, which breaks foo up into individual words by writing them into buf, the length variable has incorrect values if foo contains spaces. Watch:

    foo = "they call me jim"
    bar = "they call me jim"

    After the first iteration, bar becomes "they". The strlen of bar is 4, so length becomes 4. revword is called and works properly, as we've already established, so "yeht" is printed.

    The next iteration has length 4, foo + length is " call me jim". Inside word, spaces are ignored and after the call, bar becomes "call". Now, the strlen of bar, which is 4, is added to length, making it 8. revword is called on bar and works properly, printing "llac".

    Now the fun part. On the third iteration, length is 8 so foo + length is "l me jim". So word fails to do what was expected and bar becomes "l". revword still works properly and prints "l", but length is only incremented to 9.

    The next iteration has foo as " me jim", as we expected the last one to be. bar becomes "me" and length is increased by 2. revword prints properly as "em".

    The next iteration has foo as "e jim", once again off by one character. The effect is the same and "e" is printed, length becomes 12.

    The next iteration has foo as "jim", bar as "jim", and length as 15. "mij" is properly printed.

    Next, foo + 15 is "m", still off by one and "m" is printed, finally causing length to reach 16. On the next iteration, foo + 16 is "" and the loop terminates.

    revword is correct, but word is not because it makes changes to the string that main doesn't account for, causing a fencepost error and ultimately, jumbled output.
    My best code is written with the delete key.

  5. #20
    left crog... back when? incognito's Avatar
    Join Date
    Oct 2001
    Posts
    1,427
    Uh....perhaps I am missing something.......but are you not allowed to just stick this stuff into an array and then just reading from the back to the front?
    There are some real morons in this world please do not become one of them, do not become a victim of moronitis. PROGRAMMING IS THE FUTURE...THE FUTURE IS NOW!!!!!!!!!

    "...The only real game I thank in the world is baseball..." --Babe Ruth

    "Life is beautiful"-Don Corleone right before he died.

    "The expert on anything was once a beginner" -Baseball poster I own.


    Left cprog on 1-3-2005. Don't know when I am coming back. Thanks to those who helped me over the years.

  6. #21
    & the hat of GPL slaying Thantos's Avatar
    Join Date
    Sep 2001
    Posts
    5,681
    I think the point of the assignment is not to just reverse the entire string but reverse the order of the words in the string keeping each word in the orginal form.

  7. #22
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    My understanding was that they wanted this:

    In: "My name is Jim"

    Out: "yM eman si miJ"

    And my point was, were you to write a function to break words up correctly, like so, with the spaces appended correctly:
    Code:
    char *word( char *s)
    {
        static char buf[BUFSIZ] = {0};
        char spaces[BUFSIZ] = {0};
        int x = 0;
    
        memset( buf, 0, BUFSIZ );
        for( x = 0; s && *s && isspace( *s ) && x < BUFSIZ-1; spaces[x++] = *s++);
        for( x = 0; s && *s && !isspace( *s ) && x < BUFSIZ-1; buf[x++] = *s++);
        strcat( buf, spaces );
        return buf;
    }
    Then it does give the output I thought they wanted. (Apparently I don't read too well.) Anyway, the only thing wrong with my origional text program was the word function. Replace the origional word function with that one, and it does exactly what it was supposed to do. There is no need to pass the length along. The only problem with my word function was that I ignored spaces rather than including them as part of the word. This version would have given the desired results.

    And since a recursive version or reversing word order is already here, there's no need for me to write one. Anyway, as stated, the point was to do it differently than the "normal".

    Quzah.
    Hope is the first step on the road to disappointment.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Seg Fault in Compare Function
    By tytelizgal in forum C Programming
    Replies: 1
    Last Post: 10-25-2008, 03:06 PM
  2. please help with binary tree, urgent.
    By slickestting in forum C Programming
    Replies: 2
    Last Post: 07-22-2007, 07:55 PM
  3. brace-enclosed error
    By jdc18 in forum C++ Programming
    Replies: 53
    Last Post: 05-03-2007, 05:49 PM
  4. Wrong Output
    By egomaster69 in forum C Programming
    Replies: 7
    Last Post: 01-28-2005, 06:44 PM
  5. Using 'if' with char arrays or string objects
    By c++_n00b in forum C++ Programming
    Replies: 36
    Last Post: 06-06-2002, 09:04 PM