Why is this program a mistake? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
0

Why is this program a mistake?

When i challenged the "extra terrestrial" practice i made the program like this: https://code.sololearn.com/c88K0XyFp1x3/?ref=app But when i run it the system tells my program is wrong although it gives output that as same as expected output. Can anyone tell me where i did wrong or is it a bug in the system?

16th Oct 2023, 12:00 AM
Celvien Kurniawan
Celvien Kurniawan - avatar
3 Answers
+ 4
I don't know about C But I think this may cause an error, because in most programming languages indexing starts from 0. So in the first iteration you are trying to get out of range value. For example if the length is (5) The last item's index would be 4 not 5. Try this int main() { char a[100]; scanf("%s", a); int x = strlen(a); for (int i = 0; i < x; i++) { printf("%c", a[x - i - 1]); } return 0; }
16th Oct 2023, 12:33 AM
Alaa Aldeen Shammr
Alaa Aldeen Shammr - avatar
+ 2
Celvien Kurniawan notice that inside the loop, a[x-i] is past the end of the input string when i=0. The loop index should start with 1 instead of 0. The rest is good and will pass after that correction.
16th Oct 2023, 12:15 AM
Brian
Brian - avatar
+ 2
I see... Thank for the info. I'll try just like that.
16th Oct 2023, 3:01 AM
Celvien Kurniawan
Celvien Kurniawan - avatar