Average word length Problem. | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Average word length Problem.

My last test case is failing. Don't know why. Can anyone tell me. Here is my code 👇 https://code.sololearn.com/ctUdsqs3zyvF/?ref=app

1st Jul 2020, 6:12 PM
Aditya Raj
Aditya Raj - avatar
10 Answers
+ 4
The last test case fails because you effectively don't consider the last character of the sentence by decrementing 'n' in line 11 (the loop will stop at the second-last character, not the last). If you delete that line, everything should be fine.
1st Jul 2020, 6:21 PM
Shadow
Shadow - avatar
+ 2
Rishi You have a point, but the test cases are not terminated by a newline character, hence the difference to actual input from the console. Regarding your code, 50 characters seem awfully short for an english essay. You might want to allow for some more text.
16th Jun 2021, 2:28 PM
Shadow
Shadow - avatar
+ 2
Itzel Medina those are not numbers. The characters have ASCII values right? '0'=48,'1'=49,... So he's checking if the characters are numbers using those conditions. Same with the following numbers. You'll understand it when you see the respective ASCII values of the numbers he used
19th Jul 2021, 7:11 AM
Rishi
Rishi - avatar
+ 1
oohh now I get it, thanks! 🙏
19th Jul 2021, 2:29 PM
Itzel Medina
Itzel Medina - avatar
0
Shadow doesn't his code already correct? Because when you get a string with fgets from the stdin(for example "me"), the length will be one greater than the original length(in this case it would be 3). Then his code does consider the last character of the sentence.
16th Jun 2021, 9:57 AM
Rishi
Rishi - avatar
0
Shadow just tested it in sololearn and I was wrong. But for some reason, Cxxdroid followed my logic. Also, doesn't it make sense because an extra newline will be recorded by fgets() function?
16th Jun 2021, 10:00 AM
Rishi
Rishi - avatar
0
Shadow and if you can, see my code(fails 5th test case) https://code.sololearn.com/cZPNJwCHHz9o/?ref=app
16th Jun 2021, 10:06 AM
Rishi
Rishi - avatar
0
Shadow oh okay I'll try that
16th Jun 2021, 3:22 PM
Rishi
Rishi - avatar
0
Shadow thank you so much! It worked. It's really amazing to know that it's that 50 character length that wasn't enough. I didn't know that and was trying for days. Thank you again. Have a nice day
16th Jun 2021, 3:28 PM
Rishi
Rishi - avatar
0
Hi! Can you explain the logic behind your code? I'm very new on all this about programming and I already did the exercise, but I want understand the logic behind your code, I can't understand it 😅, why you put those numbers in the conditionals? (I'm not sure if said it right, my english isn't very good btw 😅),
19th Jul 2021, 4:58 AM
Itzel Medina
Itzel Medina - avatar