Reversing strings using strtok()
What i gotta do:
Write a program that inputs a line of text, tokenizes the line with the strtok() function and outputs the tokens in reverse order. For example the input string: "The quick brown fox jumps over the dog" would be output as: "dog the over jumps fox brown quick The".
Test your solution with the following test data:
This is, the test data for the program
The output should be:
program
the
for
data
test
the
is
This
Code so far:
Code:
int main()
{
char s1[80];
char *tokenptr;
char seps[] = " ,\t\n';
char *tokenarray[80];
int i = 0:
printf("Input the string you want tokenized: \n\n");
gets(s1);
printf("\nThe tokens in reverse order are: \n\n");
tokenptr = strtok(s1, seps);
while(tokenptr != null){
tokenarray[i++] = s1;
printf("%s\n", tokenptr);
tokenptr = strtok(null,seps);
}
return 0;
}
it works but i need it to be in reverse order
my output looks like:
This
is,
the
test
data
for
the
program
need it to be:
program
the
for
data
test
the
is
This
Any suggestions?