-
-
Notifications
You must be signed in to change notification settings - Fork 140
Implemented the loop for reading input into a string without using th… #95
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
base: main
Are you sure you want to change the base?
Changes from 1 commit
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -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; | ||
| } | ||
|
Comment on lines
+27
to
+30
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Questionable: Extra newline printed on EOF. Lines 25-28 print a newline when EOF is encountered, but not for other exit conditions (newline or buffer full). This creates inconsistent output behavior. Consider whether this extra newline is necessary or if EOF should be handled like the newline case (just break). 🤖 Prompt for AI Agents |
||
| else | ||
| s[i++] = c; | ||
| } | ||
|
|
||
| s[i] = '\0'; | ||
|
|
||
| printf("%s", s); | ||
| printf("%s\n", s); | ||
|
|
||
| return 0; | ||
| } | ||
|
|
||
Uh oh!
There was an error while loading. Please reload this page.