This is what I have so far, which almost works:
I use the strrev function to reverse the string, and then the inprev function to send each word to the strrev function individually, to reverse them back to the original orientation, but in reversed order.
Sending a pointer for the start and end of the strrev function might seem a bit silly, but it allows the same function to be used in inprev(), sending off a pointer to the start and end of individual words.

The problem is that the lack of a final space at the end of the final word means that a space is missing between that word and the preceeding one in the final string, and instead gets thrown onto the end of the last word, which was the first word in the original string. Sending off the space on the other side of the word only moves the problem elsewhere. Can anyone see a solution?

4 Answers
4

You just need to move the start pointer in the inprev function to skip the space between words. As this appears to be homework (correct me if I'm wrong) I'll just say that all you need to do is move the location of one operator.

But, this produces a problem, namely, the inprev performs a buffer overrun because the search isn't terminated properly. A better way to do it is:

while not end of string
search for start of word
start = start of word
search for end of word
strrev (start, end)

and that will take care of multiple spaces too. Also, U+0020 (ASCII 32, a space) is not the only white space character. There are standard library functions that test characters. They are in <ctype.h> and start with is..., e.g. isspace.

@Matt - No harm done. :) Easily rolled back. I agree with Skizz though - it'd be great if you could post your fixed code as an answer to help future readers to completely understand the fix.
–
razlebeAug 31 '11 at 10:23

Sometimes things get easier if you don't use pointers but offsets.
The strspn() and strcspn() library functions more or less force you to use offsets,
and deal with the end-of-string condition quite nicely.

Would you be able to add a few comments to this explaining what's happening? It's a bit hard to follow, for example, what's happening in the revword function.
–
MattSep 2 '11 at 6:10

Well: in the loop, I measure the length of consecutive whitespace (the strspn() call), and skip it. After that I measure the length of consecutive NONwhitespace (the strcspn() call) and reverse it. At the end of the loop I have the whole length of the string accumulated and reverse the whole string.
–
wildplasserSep 2 '11 at 8:34