compare.c 838 B

12345678910111213141516171819202122232425262728293031323334353637383940414243
  1. /* compare.c -- this will work */
  2. #include <stdio.h>
  3. #include <string.h> // declares strcmp()
  4. #define ANSWER "Grant"
  5. #define SIZE 40
  6. char * s_gets(char * st, int n);
  7. int main(void)
  8. {
  9. char try[SIZE];
  10. puts("Who is buried in Grant's tomb?");
  11. s_gets(try, SIZE);
  12. while (strcmp(try,ANSWER) != 0)
  13. {
  14. puts("No, that's wrong. Try again.");
  15. s_gets(try, SIZE);
  16. }
  17. puts("That's right!");
  18. return 0;
  19. }
  20. char * s_gets(char * st, int n)
  21. {
  22. char * ret_val;
  23. int i = 0;
  24. ret_val = fgets(st, n, stdin);
  25. if (ret_val)
  26. {
  27. while (st[i] != '\n' && st[i] != '\0')
  28. i++;
  29. if (st[i] == '\n')
  30. st[i] = '\0';
  31. else // must have words[i] == '\0'
  32. while (getchar() != '\n')
  33. continue;
  34. }
  35. return ret_val;
  36. }