+ 1

How do i fix this code?

When i tackled the code coach challenge (Even Numbers [https://www.sololearn.com/coach/69?ref=app]), i made the code like this: https://sololearn.com/compiler-playground/ch0FB2xtd45j/?ref=app But this code shows error at the 4th case in codecoach output. I have a guest that there is an additional space in my output but i don't know how i should erase it.

17th Sep 2024, 9:56 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
12 Réponses
+ 4
increase the capacity of the char arrays in line 6
17th Sep 2024, 10:12 AM
Lisa
Lisa - avatar
+ 3
Aysha , it is not seen as a helpful behavior when we are going to post a code, as long as the op has not shown his attempt here. it is helpful to give hints and tips, so that the op has a chance to find a solution by himself.
17th Sep 2024, 2:35 PM
Lothar
Lothar - avatar
+ 2
Celvien Kurniawan the extra trailing space is accepted by Code Coach. My own code passes with an extra space. As Lisa pointed out, the char arrays are too small to contain the entire input string. After that small adjustment your code passes all tests. The task can be solved more simply and without char arrays by letting scanf do the parsing one number at a time directly into an int variable. In fact, one variable is all that is needed to solve it! int x; while (scanf("%d ", &x) != EOF) { ... }
17th Sep 2024, 8:05 PM
Brian
Brian - avatar
18th Sep 2024, 4:30 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
+ 2
I had a much more complicated code, because I had to take care of that extra space which seemed not to be necessary, because Code Coach accepted that extra space😂
18th Sep 2024, 7:04 AM
Jan
Jan - avatar
+ 1
Here is the solution #include <stdio.h> #include <stdlib.h> #include <string.h> int main() { char s[20], a[20], *p = NULL; int x; int first = 1; // Flag to track if it's the first even number fgets(s, sizeof(s), stdin); p = strtok(s, " "); while (p != NULL) { x = atoi(p); if ((x % 2) == 0) { if (!first) { printf(" "); // Print space before even number, except for the first one } strcpy(a, p); printf("%s", a); first = 0; // Set first flag to 0 after printing the first even number } p = strtok(NULL, " "); } printf("end"); return 0; } Here is the explanation to the change made first flag: This variable is used to ensure that a space is printed only between even numbers, but not after the last number or before the first one. Space logic: A space is printed only when the flag first is 0 (i.e., after the first even number has been printed).
17th Sep 2024, 10:52 AM
Aysha
Aysha - avatar
+ 1
Aysha Your code is buggy. It only prints the first even number, because your flag solution prevents it from printing the rest of the even numbers.
17th Sep 2024, 6:38 PM
Jan
Jan - avatar
+ 1
Jan I had tested the code before posting it here I guess you typed the input line by line instead of a single line with space between them as mentioned by Celvien Kurniawan in his code. I hope you check it once again and thanks for informing me about the bug.
17th Sep 2024, 6:51 PM
Aysha
Aysha - avatar
+ 1
Aysha Yes okay, I typed line by line. It works with the single line. That playground sometimes confuse me. I normally write that kind of code on a pc.
17th Sep 2024, 7:33 PM
Jan
Jan - avatar
+ 1
It would be enough to set the char array to 100. I did that in my own solution long time ago.
17th Sep 2024, 9:10 PM
Jan
Jan - avatar
0
Thanks everyone for telling me where the bug is. Actually, i didn't think the array has problem before i read these answers, so was so confused at that time. Even chatgpt seems failed to point it out, too.
18th Sep 2024, 2:39 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
0
Aysha hmmm. That code seems like from chatgpt, i assume. Actually, i also ask chatgpt for this problem, but its (the answers) pretty hard to understand for me.
18th Sep 2024, 2:43 AM
Celvien Kurniawan
Celvien Kurniawan - avatar