The C Programming Language

Chapter 4 - Functions and Program Structure 8

mmresult 2009. 3. 27. 13:57


Answer to Exercise 4-8, page 79
Solution by Steven Huang
Suppose there will never be more than one character of pushback. Modify getch and ungetch accordingly.

 


/* K&R Exercise 4-8 */
/* Steven Huang */

#include <stdio.h>

int buf = EOF; /* buffer for ungetch */

int getch(void) /* get a (possibly pushed back) character */
{
  int temp;

  if (buf != EOF) {
    temp = buf;
    buf = EOF;
  } else {
    temp = getchar();
  }
  return temp;                         
}
 
void ungetch(int c) /* push character back on input */
{
  if(buf != EOF)
    printf("ungetch: too many characters\n");
  else      
    buf = c;
}
 
int main(void)
{
  int c;

  while ((c = getch()) != EOF) {
    if (c == '/') {
      putchar(c);
      if ((c = getch()) == '*') {
        ungetch('!');
      }        
    }
    putchar(c);              
  }
  return 0;
}