fork download
  1. #include <stdio.h>
  2. #include <string.h>
  3.  
  4. const int STORAGE = 1024;
  5.  
  6. int getword(char * w) {
  7. int iochar = 0;
  8. int index = 0;
  9. int numberofchars = 0;
  10.  
  11. while ((iochar = getchar()) != EOF) {
  12. if (iochar != ' ' && iochar != '\n') {
  13. w[index] = iochar;
  14. index++;
  15. numberofchars++;
  16. } else {
  17. w[index] = '\0';
  18. if (strcmp(w, "done") == 0) {
  19. return -1;
  20. } else {
  21. return numberofchars;
  22. }
  23. }
  24. } //after while loop
  25. w[index] = '\0';
  26. return (numberofchars > 0 ? numberofchars : -1);
  27. } // end of function
  28.  
  29.  
  30.  
  31. int main()
  32. {
  33. int c;
  34. char s[STORAGE];
  35.  
  36. for (;;) {
  37. c = getword(s);
  38. if (c == -1) break;
  39. printf("n=%d, s=[%s]\n", c, s);
  40. }
  41. }
Success #stdin #stdout 0s 4476KB
stdin
trickier to master
hello world who is it hi
stdout
n=8, s=[trickier]
n=2, s=[to]
n=6, s=[master]
n=5, s=[hello]
n=5, s=[world]
n=3, s=[who]
n=2, s=[is]
n=2, s=[it]
n=2, s=[hi]