fork download
  1. /* K&R Exercise 4-8 */
  2. /* Steven Huang */
  3.  
  4. #include <stdio.h>
  5.  
  6. int buf = EOF; /* buffer for ungetch */
  7.  
  8. int getch(void) /* get a (possibly pushed back) character */
  9. {
  10. int temp;
  11.  
  12. if (buf != EOF) {
  13. temp = buf;
  14. buf = EOF;
  15. } else {
  16. temp = getchar();
  17. }
  18. return temp;
  19. }
  20.  
  21. void ungetch(int c) /* push character back on input */
  22. {
  23. if(buf != EOF)
  24. printf("ungetch: too many characters\n");
  25. else
  26. buf = c;
  27. }
  28.  
  29. int main(void)
  30. {
  31. int c;
  32.  
  33. while ((c = getch()) != EOF) {
  34. if (c == '/') {
  35. putchar(c);
  36. if ((c = getch()) == '*') {
  37. ungetch('!');
  38. }
  39. }
  40. putchar(c);
  41. }
  42. return 0;
  43. }
Success #stdin #stdout 0.01s 1724KB
stdin
stdout