diff --git a/chapter_2/exercise_2_02/loop.c b/chapter_2/exercise_2_02/loop.c index 5cfbb05..2b60f5b 100644 --- a/chapter_2/exercise_2_02/loop.c +++ b/chapter_2/exercise_2_02/loop.c @@ -5,31 +5,25 @@ int main(void) { char s[MAXLINE]; - - // int i; - // int c; - // for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); ++i) - // { - // s[i] = c; - // } - - int i = 0; - int loop = 1; - while (loop) - { - char c = getchar(); - - if (i >= (MAXLINE - 1) || c == '\n' || c == EOF) - { - loop = 0; + int c, i = 0; + + while (1){ + if (i >= MAXLINE - 1) + break; + + c = getchar(); + + if (c == '\n') + break; + else if (c == EOF) + break; + else + s[i++] = c; } + s[i] = '\0'; - s[i++] = c; - } - - s[i] = '\0'; + printf("%s\n", s); - printf("%s", s); return 0; }