First, we create a variable called finished which can be set
to NO
or YES
(we create these constants using
an enum
.) We then enter a while
-loop that has
the condition !finished
, which means the loop will
terminate once finished is equal to YES
. Then,
within every iteration of the loop, we check if the three conditions
are true using a series of nested if
-statements, and
execute s[i] = c
if all three are satisfied. However, if
any one of them is not satisfied at any point, we set
finished to YES
, terminating the loop.
enum boolean { NO, YES };
/* getline: read a line into s, return length */
int my_getline(char s[], int lim)
{
int c;
int i = 0;
int finished = NO;
while (!finished) {
if (i < lim - 1) {
if ((c = getchar()) != EOF) {
if (c != '\n') {
s[i] = c;
++i;
} else
finished = YES;
} else
finished = YES;
} else
finished = YES;
}
...
}