Added cbd_rand. Generates x random numbers in y columns. Interactive program.
[C-Programming-Examples.git] / ex_1-19.c
blob0bc69ce930b16fffa9718f475fcd1af13065f057
1 /*
3 maxline
6 function: discard newline
7 for loop through buffer looking for terminator
8 if current postion is newline, set position to terminator
11 function: reverse
12 for find end of buffer as integer
13 move back one space
14 for loop reverse through string printing out chars one at a time
18 function: getline
19 for get chars until buffer is full
20 if last char is newline add to end of buffer
21 terminate buffer with '\0'
24 function: main
25 while getline > 0
26 disgard new line
27 reverse line
28 print line
34 #include <stdio.h>
36 #define MAXLEN 1024
41 loop throguh buffer
42 convert newlines into nulls marking end of array
45 int discard(char buffer[])
47 int i;
48 for(i = 0; buffer[i] != '\0'; i++) /* loop until we find NULL */
50 if(buffer[i] == '\n') { buffer[i] = '\0'; } /* convert newlines into NULL */
56 reverse
58 loop through buffer reversing chars
61 int reverse(char buffer[])
63 char ch;
64 int i,j;
65 for(j = 0; buffer[j] != '\0'; ++j) { } /* find length of buffer */
66 --j; /* move back one space */
68 /* loop though length of array reversing front to back chars */
69 for(i = 0; i < j; i++)
71 ch = buffer[i];
72 buffer[i] = buffer[j];
73 buffer[j] = ch;
74 --j;
80 getine
82 get input passed to program and store in buffer
83 check along the way that the input has not run out
84 do this until we reach a newline and then terminate with NULL
87 int getline(char buffer[], int limit)
89 int i;
90 char c;
91 for(i = 0; i < limit - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
93 buffer[i] = c;
96 if(c == '\n') { buffer[i++] = c; }
98 buffer[i] = '\0'; /* terminate string at this position */
100 return i; /* return number of chars found in this line */
103 int main()
105 char buffer[MAXLEN];
107 while(getline(buffer, sizeof buffer) > 0)
109 // printf("getline\n");
110 discard(buffer);
111 reverse(buffer);
112 printf("%s\n", buffer);
114 return 0;