fgets3.c 594 B

12345678910111213141516171819202122232425
  1. /* fgets3.c -- using fgets() */
  2. #include <stdio.h>
  3. #define STLEN 10
  4. int main(void)
  5. {
  6. char words[STLEN];
  7. int i;
  8. puts("Enter strings (empty line to quit):");
  9. while (fgets(words, STLEN, stdin) != NULL
  10. && words[0] != '\n')
  11. {
  12. i = 0;
  13. while (words[i] != '\n' && words[i] != '\0')
  14. i++;
  15. if (words[i] == '\n')
  16. words[i] = '\0';
  17. else // must have words[i] == '\0'
  18. while (getchar() != '\n')
  19. continue;
  20. puts(words);
  21. }
  22. puts("done");
  23. return 0;
  24. }