Skip to content

Commit cafa649

Browse files
authored
Simplify character input loop in loop.c
since they have asked in the question to not to use the relational operators, I have written the code for the loop without the relational operators(&& and ||)
1 parent 2f1fed8 commit cafa649

File tree

1 file changed

+17
-22
lines changed

1 file changed

+17
-22
lines changed

chapter_2/exercise_2_02/loop.c

Lines changed: 17 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -5,31 +5,26 @@
55
int main(void)
66
{
77
char s[MAXLINE];
8-
9-
// int i;
10-
// int c;
11-
// for (i = 0; (i < MAXLINE - 1) * ((c = getchar()) != '\n') * (c != EOF); ++i)
12-
// {
13-
// s[i] = c;
14-
// }
15-
16-
int i = 0;
17-
int loop = 1;
18-
while (loop)
19-
{
20-
char c = getchar();
21-
22-
if (i >= (MAXLINE - 1) || c == '\n' || c == EOF)
23-
{
24-
loop = 0;
8+
int c, i = 0;
9+
10+
while (1){
11+
if (i >= MAXLINE - 1)
12+
break;
13+
14+
c = getchar();
15+
16+
if (c == '\n')
17+
break;
18+
else if (c == EOF)
19+
break;
20+
else
21+
s[i] = c;
22+
23+
++i;
2524
}
2625

27-
s[i++] = c;
28-
}
29-
30-
s[i] = '\0';
26+
printf("\n%s\n", s);
3127

32-
printf("%s", s);
3328

3429
return 0;
3530
}

0 commit comments

Comments
 (0)