-
Notifications
You must be signed in to change notification settings - Fork 67
/
Copy pathex_3-3.c
63 lines (53 loc) · 1.16 KB
/
ex_3-3.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
#include <stdio.h>
/* get next group of tokens from stdin */
int get_tokens(int *dir, char *start, char *stop){
int status = 0;
char c = '\0';
if((c = getchar()) != EOF) { ++status; }
if(c == '-') {
*dir = 1;
if((*start = getchar()) != EOF) { ++status; }
if(getchar() != EOF) { ++status; } // should be a '-'
if((*stop = getchar()) != EOF) { ++status; }
} else {
*dir = 0;
*start = c;
if(getchar() != EOF) { ++status; } // should be a '-'
if((*stop = getchar()) != EOF) { ++status; }
}
if(status == 4 && *dir == 1) { return 1; }
if(status == 3 && *dir == 0) { return 1; }
return -1;
}
/*
print out series of chars based on spec.
dir = direction to print
start and stop are chars in ASCII to begin and end with.
*/
void print_tokens(int *dir, char *start, char *stop)
{
if(*dir == 0){ // foreward
while(*start != *stop + 1)
{
putchar(*start);
*start = *start + 1;
}
} else { // reverse
while(*start != *stop - 1)
{
putchar(*start);
*start = *start - 1;
}
}
}
int main()
{
char start, stop;
int dir;
while(get_tokens(&dir, &start, &stop) == 1)
{
print_tokens(&dir, &start, &stop);
putchar('\n');
}
return 0;
}