Originally Posted by
TheBigH
Okay, so what you need to do is break the string into words. Use strtok for this.
Then store the individual words in an array, and print that array in reverse order.
Thanks, ok i was able to get the words separated but i cant get them to print backwards.. this is what i got so far:
Code:
#include <stdio.h>
#include <string.h>
int main (void)
{
char buffer [1023];
char *split;
char *inverse;
char end;
int i;
printf ("Please enter a string: ");
fgets(buffer,1023, stdin);
split = strtok(buffer," ");
end = strlen(split) - 1;
for (i = 0; i < end ; i ++)
{
inverse[i] = split;
// printf("%s ", split);
split = strtok (NULL, ", ");
}
for (i = end; i > 0; i--)
printf("%s ", inverse[i]);
return 0;
}
but i get a compiler error
Code:
reverseWords.c: In function ‘main’:
reverseWords.c:19: warning: assignment makes integer from pointer without a cast
reverseWords.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’
reverseWords.c:24: warning: format ‘%s’ expects type ‘char *’, but argument 2 has type ‘int’