Code:
#include <stdio.h>
#define MAXLINE 100
static void reverse(char *str)
{
char word[MAXLINE];
char *w = word;
while (*str == ' ')
++str; //let's str skip over white space.
if ( *str == '\0' ) //str has found the end of string char, we're done here.
return;
while (*str != '\0' && *str != ' ') //if str is pointing at a letter, not a space, and
*w++ = *str++; //not an end of string char, then
*w = '\0'; //we've got another w, and increment str, please!
reverse(str) //we're not at the end, more words to follow, so
printf("%s ",word); //mark this word just found, with an end of string char
} //and print it.
int main ( )
{
char str[MAXLINE];
printf("Enter a sentence:\n");
fflush(stdout);
gets(str);
reverse(str);
return 0;
}
Hope the above comments help. Str is a pointer, and you can add to pointers, to "walk" through data. The pointer will know the size of the thing it's pointing to, so it will be sized right for the job.