From fb49ce7ad9e3000f4e08a477621e2ea770f53a3d Mon Sep 17 00:00:00 2001 From: ayush <152896667+kkaname@users.noreply.github.com> Date: Wed, 10 Dec 2025 05:03:26 +0530 Subject: [PATCH 1/3] Implemented the loop for reading input into a string without using the relational operators. As per asked in the excercise question, I have implemented the loop using while loop and break statement without using any relational operator(&& and ||). --- chapter_2/exercise_2_02/loop.c | 31 ++++++++++++++++++------------- 1 file changed, 18 insertions(+), 13 deletions(-) diff --git a/chapter_2/exercise_2_02/loop.c b/chapter_2/exercise_2_02/loop.c index 5b127a6..fb07177 100644 --- a/chapter_2/exercise_2_02/loop.c +++ b/chapter_2/exercise_2_02/loop.c @@ -4,30 +4,35 @@ int main(void) { char s[MAXLINE]; + int c, i = 0; - // int i; - // int c; - // for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); - // ++i) + // int i, c; + // for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); ++i) // { // s[i] = c; // } - - int i = 0; - int loop = 1; - while (loop) { + + while (1) { + if (i >= MAXLINE - 1){ + break; + } + char c = getchar(); - if (i >= (MAXLINE - 1) || c == '\n' || c == EOF) { - loop = 0; + if (c == '\n'){ + break; } - - s[i++] = c; + else if (c == EOF){ + printf("\n"); + break; + } + else + s[i++] = c; } s[i] = '\0'; - printf("%s", s); + printf("%s\n", s); return 0; } From 603ddadb38140ac9a6be625a70af979c3c405628 Mon Sep 17 00:00:00 2001 From: ayush <152896667+kkaname@users.noreply.github.com> Date: Wed, 10 Dec 2025 05:07:23 +0530 Subject: [PATCH 2/3] Fix variable declaration for character input --- chapter_2/exercise_2_02/loop.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/chapter_2/exercise_2_02/loop.c b/chapter_2/exercise_2_02/loop.c index fb07177..b10c3e0 100644 --- a/chapter_2/exercise_2_02/loop.c +++ b/chapter_2/exercise_2_02/loop.c @@ -17,7 +17,7 @@ int main(void) { break; } - char c = getchar(); + c = getchar(); if (c == '\n'){ break; From 54e8e54403b6f5fbe4c19a1a67f00c39cbbdf73f Mon Sep 17 00:00:00 2001 From: ayush <152896667+kkaname@users.noreply.github.com> Date: Wed, 10 Dec 2025 16:10:25 +0530 Subject: [PATCH 3/3] Fix: enforce single-variable declarations to align with repo style. Refactor variable declarations for clarity. --- chapter_2/exercise_2_02/loop.c | 6 ++++-- 1 file changed, 4 insertions(+), 2 deletions(-) diff --git a/chapter_2/exercise_2_02/loop.c b/chapter_2/exercise_2_02/loop.c index b10c3e0..fb91de7 100644 --- a/chapter_2/exercise_2_02/loop.c +++ b/chapter_2/exercise_2_02/loop.c @@ -4,9 +4,11 @@ int main(void) { char s[MAXLINE]; - int c, i = 0; + int c; + int i = 0; - // int i, c; + // int i; + //int c; // for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); ++i) // { // s[i] = c;