Hi, all--
I'm writing a program to reverse a string word by word, e.g. "some words" becomes "words some", and I'm having some trouble getting it to ignore the null portion of the array. Here's my code:
Code:
int main()
{
char *wordptr[81], s1[81], s2[81];
int i = 0;
printf("Input: ");
fgets(s1, 80, stdin);
wordptr[i] = strtok(s1," \n");
i++;
while(strtok(NULL, " \n") != NULL)
{
wordptr[i] = strtok(NULL, " \n");
i++;
}//break string into tokens
printf("Reversed: ");
for(i = i; i >= 0; i--)
{
printf("%s ", wordptr[i]);
}//print words marked by pointers in wordptr in reverse
printf("\n");
return 0;
}//main
And here's the output I'm getting:
Code:
teyla:~ quasigreat$ ~quasigreat/a.out
Input: bibbity boppity boo
Reversed: boo bibbity
teyla:~ quasigreat$ ~quasigreat/a.out
Input: birds and bees
Reversed: bees birds
teyla:~ quasigreat$ ~quasigreat/a.out
Input: now is the time for all good men to come to the aid of their country
Reversed: (null) their aid to to good for the now
I'm not sure why it seems to be skipping every other word. Help is much appreciated.