diff --git a/chapter_2/exercise_2_02/loop.c b/chapter_2/exercise_2_02/loop.c index 5b127a6..fb91de7 100644 --- a/chapter_2/exercise_2_02/loop.c +++ b/chapter_2/exercise_2_02/loop.c @@ -4,30 +4,37 @@ int main(void) { char s[MAXLINE]; + int c; + int i = 0; // int i; - // int c; - // for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); - // ++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; + + while (1) { + if (i >= MAXLINE - 1){ + break; } + + c = getchar(); - s[i++] = c; + if (c == '\n'){ + break; + } + else if (c == EOF){ + printf("\n"); + break; + } + else + s[i++] = c; } s[i] = '\0'; - printf("%s", s); + printf("%s\n", s); return 0; }