quit_chk.c 869 B

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. /* quit_chk.c -- beginning of some program */
  2. #include <stdio.h>
  3. #include <string.h>
  4. #define SIZE 80
  5. #define LIM 10
  6. #define STOP "quit"
  7. char * s_gets(char * st, int n);
  8. int main(void)
  9. {
  10. char input[LIM][SIZE];
  11. int ct = 0;
  12. printf("Enter up to %d lines (type quit to quit):\n", LIM);
  13. while (ct < LIM && s_gets(input[ct], SIZE) != NULL &&
  14. strcmp(input[ct],STOP) != 0)
  15. {
  16. ct++;
  17. }
  18. printf("%d strings entered\n", ct);
  19. return 0;
  20. }
  21. char * s_gets(char * st, int n)
  22. {
  23. char * ret_val;
  24. int i = 0;
  25. ret_val = fgets(st, n, stdin);
  26. if (ret_val)
  27. {
  28. while (st[i] != '\n' && st[i] != '\0')
  29. i++;
  30. if (st[i] == '\n')
  31. st[i] = '\0';
  32. else // must have words[i] == '\0'
  33. while (getchar() != '\n')
  34. continue;
  35. }
  36. return ret_val;
  37. }