Added cbd_rand. Generates x random numbers in y columns. Interactive program.
[C-Programming-Examples.git] / ex_3-3.c
blob6077de589910d529456be2da29e02f6cb6bb747d
1 #include <stdio.h>
3 /* get next group of tokens from stdin */
4 int get_tokens(int *dir, char *start, char *stop){
6 int status = 0;
7 char c = '\0';
8 if((c = getchar()) != EOF) { ++status; }
10 if(c == '-') {
11 *dir = 1;
12 if((*start = getchar()) != EOF) { ++status; }
13 if(getchar() != EOF) { ++status; } // should be a '-'
14 if((*stop = getchar()) != EOF) { ++status; }
15 } else {
16 *dir = 0;
17 *start = c;
18 if(getchar() != EOF) { ++status; } // should be a '-'
19 if((*stop = getchar()) != EOF) { ++status; }
22 if(status == 4 && *dir == 1) { return 1; }
23 if(status == 3 && *dir == 0) { return 1; }
24 return -1;
29 print out series of chars based on spec.
30 dir = direction to print
31 start and stop are chars in ASCII to begin and end with.
34 void print_tokens(int *dir, char *start, char *stop)
36 if(*dir == 0){ // foreward
37 while(*start != *stop + 1)
39 putchar(*start);
40 *start = *start + 1;
42 } else { // reverse
44 while(*start != *stop - 1)
46 putchar(*start);
47 *start = *start - 1;
52 int main()
54 char start, stop;
55 int dir;
57 while(get_tokens(&dir, &start, &stop) == 1)
59 print_tokens(&dir, &start, &stop);
60 putchar('\n');
62 return 0;