K&R Exercises

Exercise 4-7: Push back a whole string

Write a routine ungets(s) that will push back an entire string onto the input. Should ungets know about buf and bufp, or should it just use ungetch?

#define BUFSIZE 100

char buf[BUFSIZE];    /* buffer for ungetch */
int bufp = 0;         /* next free position in buf */

int getch(void)  /* get a (possibly pushed-back) character */
{
    return (bufp > 0) ? buf[--bufp] : getchar();
}

void ungetch(int c)   /* push character back on input */
{
    if (bufp >= BUFSIZE)
        printf("ungetch: too many characters\n");
    else
        buf[bufp++] = c;
}

Use it in a program that prints each input line twice: read the line with getch, print it, push it back with ungets, then read and print it again. Lines are at most 1000 characters.

Test Cases

· space→ tab↵ newline

Submissions

No submissions yet