Skip to content

Commit fb49ce7

Browse files
authored
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 ||).
1 parent 396a2cd commit fb49ce7

File tree

1 file changed

+18
-13
lines changed

1 file changed

+18
-13
lines changed

chapter_2/exercise_2_02/loop.c

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -4,30 +4,35 @@
44

55
int main(void) {
66
char s[MAXLINE];
7+
int c, i = 0;
78

8-
// int i;
9-
// int c;
10-
// for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF);
11-
// ++i)
9+
// int i, c;
10+
// for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); ++i)
1211
// {
1312
// s[i] = c;
1413
// }
15-
16-
int i = 0;
17-
int loop = 1;
18-
while (loop) {
14+
15+
while (1) {
16+
if (i >= MAXLINE - 1){
17+
break;
18+
}
19+
1920
char c = getchar();
2021

21-
if (i >= (MAXLINE - 1) || c == '\n' || c == EOF) {
22-
loop = 0;
22+
if (c == '\n'){
23+
break;
2324
}
24-
25-
s[i++] = c;
25+
else if (c == EOF){
26+
printf("\n");
27+
break;
28+
}
29+
else
30+
s[i++] = c;
2631
}
2732

2833
s[i] = '\0';
2934

30-
printf("%s", s);
35+
printf("%s\n", s);
3136

3237
return 0;
3338
}

0 commit comments

Comments
 (0)