-
Notifications
You must be signed in to change notification settings - Fork 0
/
ungets.c
65 lines (51 loc) · 1.27 KB
/
ungets.c
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
54
55
56
57
58
59
60
61
62
63
64
65
/* Exercise 4-7. 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? */
#include "stdio.h"
#include "string.h"
#define BUFSIZE 100
#define STRSIZE 1000
#define STRTARGET "love"
#define STRAPPND " Mia"
char getch(void);
char ungetch(char c);
void ungets(const char s[]);
int main(void) {
char s[STRSIZE];
char d[] = STRTARGET;
size_t i, j;
for (i = 0; (s[i] = getch()) != '\n'; i++) {
for (j = 0; s[i] == d[j] && j < strlen(d); j++)
s[++i] = getch();
if (strlen(d) == j) {
ungetch(s[i--]);
ungets(STRAPPND);
}
}
s[i] = '\0';
printf("%s\n", s);
return 0;
}
char buf[BUFSIZE];
size_t bufp;
/* `getch` try to get a character from buffer first, then from `getchar` if
* buffer is empty */
char getch(void) {
return bufp > 0 ? buf[--bufp] : getchar();
}
/* push a character into buffer */
char ungetch(char c) {
if (bufp < BUFSIZE) {
buf[bufp++] = c;
return 1; // pushed successfully
}
else
printf("no empty space in buffer\n");
return 0;
}
/* push a string into buffer */
void ungets(const char s[]) {
int i;
for (i = strlen(s) - 1; ungetch(s[i]) && i > 0; i--)
;
}