This is a code in C to find the average word length but it does not work beyond case 1.......why? | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 3

This is a code in C to find the average word length but it does not work beyond case 1.......why?

#include <stdio.h> #include<string.h> int main() { int l,s; char a[100]={0}; fgets(a,100,stdin); for(int i=0;i<98;++i) { if((a[i]<=90 && 65<=a[i]) || (a[i]<=122 && a[i]>=97)) { l++; } } for(int i=0;i<98;++i) { if(a[i]==' ') { s++; } } float c; c=(l/(s+1))+0.5; int d; d=c; printf("%d",d); return 0; }

3rd Apr 2020, 5:01 PM
Jenil Kanani
Jenil Kanani - avatar
2 Answers
+ 3
Round up means 3.000000000001 becomes 4 or ceiling. Your code is doing a normal round. Use ceil for it. https://www.programiz.com/cpp-programming/library-function/cmath/ceil
3rd Apr 2020, 10:27 PM
John Wells
John Wells - avatar
+ 2
Thank you.I think I misunderstood the question.
4th Apr 2020, 5:38 AM
Jenil Kanani
Jenil Kanani - avatar