I agree it does not reverse words, but I fail to understand why it does not reverse a string.Originally Posted by kermit
I agree it does not reverse words, but I fail to understand why it does not reverse a string.Originally Posted by kermit
The one who says it cannot be done should never interrupt the one who is doing it.
>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.
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.Originally Posted by Prelude
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.
>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.
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.
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.
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:
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.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; }
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.