-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy path4-5.c
More file actions
54 lines (44 loc) · 682 Bytes
/
4-5.c
File metadata and controls
54 lines (44 loc) · 682 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#include <u.h>
#include <libc.h>
#include <stdio.h>
#include <ctype.h>
#define BUFSIZE 100
char buf[BUFSIZE];
int bufp = 0;
int getch(void);
void ungetch(int);
void ungets(char[]);
void
main(void)
{
printf("KR 4-5\n");
char input[BUFSIZE] = { 'i', 'n', 'p', 'u', 't' };
print("before push\n");
print("buf: %s\n", buf);
ungets(input);
print("after push\n");
print("buf: %s\n", buf);
}
int
getch(void)
{
return (bufp > 0) ? buf[--bufp] : getchar();
}
void
ungetch(int c)
{
if (bufp >= BUFSIZE)
print("ungetch: too many characters\n");
else
buf[bufp++] = c;
}
void
ungets(char s[])
{
int c, i;
i = 0;
while (s[i] != 0) {
c = s[i++];
ungetch(c);
}
}