+ 3

To find average word length

I couldn't get two cases correct. What could be reason. Can anyone verify it. Thanks

8th Mar 2021, 8:05 PM
Karma Gayleg
Karma Gayleg - avatar
12 Respuestas
8th Mar 2021, 8:08 PM
Atul [Inactive]
+ 2
Hi! please specify the programming language, the task condition, and your code attempt
8th Mar 2021, 8:17 PM
Yaroslav Vernigora
Yaroslav Vernigora - avatar
+ 2
Karma Gayley Formula should be awl = total_characters / total_words; Where total_characters should be without special character. Then use Math.ceil(awl) to get nearest whole number.
8th Mar 2021, 8:29 PM
A͢J
A͢J - avatar
+ 1
I used Java. And here is my code.
8th Mar 2021, 8:18 PM
Karma Gayleg
Karma Gayleg - avatar
+ 1
import java.util.*; public class Program { public static void main(String[] args) { Scanner sc=new Scanner(System.in); String s=sc.nextLine(); double sum=0;double av=0;int count=0; String c[]=s.split(" "); for(String word:c){ int wordlen=word.length(); sum=sum+wordlen; count++; } av=Math.round((double)(sum/count)); System.out.println((int)av); }}
8th Mar 2021, 8:18 PM
Karma Gayleg
Karma Gayleg - avatar
+ 1
Karma Gayley Special characters should not be count to get average word length.
8th Mar 2021, 8:24 PM
A͢J
A͢J - avatar
+ 1
// The Java version: import java.util.Scanner; public class Java { public static void main(String[] args) { Scanner p = new Scanner(System.in); String a[] = p.nextLine().split(" "); char b[] = ",?!.".toCharArray(); double c = 0; for (String x: a) { for (char y: b) { x = x.replace(Character.toString(y), ""); } c += x.length(); } String h = Double.toString(Math.ceil(c / a.length)); System.out.print((h.split("\\."))[0]); } }
9th Mar 2021, 9:25 AM
Calvin Thomas
Calvin Thomas - avatar
+ 1
i use text = input() word = text.split() only_alpha = text.replace(" ", "") print(len(only_alpha) / len(word))
10th May 2021, 8:14 AM
Reza Hadi Saputra
Reza Hadi Saputra - avatar
0
I did that. I couldn't get the first case right.
8th Mar 2021, 8:42 PM
Karma Gayleg
Karma Gayleg - avatar
0
// The C version: #include <stdio.h> int main() { char a[100]; scanf("%[^\n]s", a); int len, nums; nums = (a[0] == '\0') ? 0 : 1; for (int i=0;a[i]!='\0';i++) { if (a[i] == ' ' && a[i+1] != '\0') { nums++; continue; } len++; switch (a[i]) { case '?': case '!': case '.': case ',': case '-': len--; break; } } printf("%d", len/nums + (len%nums!=0)); return 0; } # The Python version: a = input().replace("?", "").replace(".", "") x = (len(a)-a.count(" "))/(a.count(" ")+1) print(int(x) + (x>x//1))
9th Mar 2021, 9:25 AM
Calvin Thomas
Calvin Thomas - avatar
0
Karma Gayley Need to remove any punctuations marks from words.. (just count alphabets only ) And use Math.ceil(result); hope it works..
9th Mar 2021, 9:46 AM
Jayakrishna 🇮🇳
0
# The Python version from math import ceil s=input().split() c=0 d=0 for i in range(len(s)): for j in range(len(s[i])): if(s[i][j]!='?'): c+=1 d+=1 x=c/d print(ceil(x)) Happy coding 👍😃
9th Mar 2021, 9:01 PM
❤️😍Prerana😍❤️
❤️😍Prerana😍❤️ - avatar