fork download
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. #define S_SPACE 0
  6. #define S_CHAR 1
  7.  
  8. void substr(char buf[], char *str, int wstart, int wend) {
  9. strncpy(buf, str + wstart, wend - wstart + 1);
  10. buf[wend - wstart + 1] = '\0';
  11. }
  12.  
  13. main()
  14. {
  15. char *s = "It was a pleasant evening. A boy and a girl sat in the park.";
  16. int i, j, state, newstate;
  17. char *caption;
  18. char status[80], wordbuf[80];
  19. int wordstart, wordend, wordlen;
  20.  
  21. i = strlen(s);
  22. state = isspace(s[i]) ? S_SPACE : S_CHAR;
  23. --i;
  24. wordend = i;
  25.  
  26. while (i >= 0) {
  27. newstate = isspace(s[i]) ? S_SPACE : S_CHAR;
  28. if (newstate != state) {
  29. if (newstate == S_SPACE) {
  30. substr(wordbuf, s, i + 1, wordend);
  31. printf("%s ", wordbuf);
  32. }
  33. else {
  34. wordend = i;
  35. }
  36. state = newstate;
  37. }
  38. else {
  39. }
  40. --i;
  41. }
  42.  
  43. // Dump the remains if still in char state
  44. if (state == S_CHAR) {
  45. substr(wordbuf, s, 0, wordend);
  46. printf("%s\n", wordbuf);
  47. }
  48.  
  49. return 0;
  50. }
  51.  
Success #stdin #stdout 0.01s 1720KB
stdin
Standard input is empty
stdout
park. the in sat girl a and boy A evening. pleasant a was It