fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. #define SIZE 300
  5.  
  6. int main(void)
  7. {
  8. char sentence[SIZE];
  9. char *words[100];
  10.  
  11. int count = 0;
  12.  
  13. printf("Enter a line of text:\n");
  14. fgets(sentence, SIZE, stdin);
  15.  
  16. sentence[strcspn(sentence, "\n")] = '\0';
  17.  
  18. char *tokenPtr = strtok(sentence, " ");
  19.  
  20. while (tokenPtr != NULL)
  21. {
  22. words[count] = tokenPtr;
  23. ++count;
  24.  
  25. tokenPtr = strtok(NULL, " ");
  26. }
  27.  
  28. printf("\nThe sentence with words reversed is:\n");
  29.  
  30. for (int i = count - 1; i >= 0; --i)
  31. {
  32. printf("%s", words[i]);
  33.  
  34. if (i != 0)
  35. {
  36. printf(" ");
  37. }
  38. }
  39.  
  40. puts("");
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 5352KB
stdin
Standard input is empty
stdout
Enter a line of text:

The sentence with words reversed is:
�Z��