+ 1

Please help me with my code

Can you please tell me where I got it wrong? The code is for "Average Word Length". #include <iostream> #include <cmath> using namespace std; int main() { string s; float k = 0; float j = 1; getline(cin, s); for (int i = 0; i < s.length(); i++) { if (s[i]!=' ') { k+=1; } else { j+=1; } } cout << ceil(k/j); return 0; }

15th Feb 2022, 12:24 PM
adeadaccount
3 Answers
+ 2
Your counting include special characters like '?' Just count the letters and divide by no. of space #include <iostream> #include <cmath> using namespace std; int main() { string s; float k = 0; float j = 1; getline(cin, s); for (int i = 0; i < s.length(); i++) { if ((s[i]>='a' && s[i]<='z') || (s[i]>='A' && s[i]<='Z')) { k+=1; } if(s[i]==' ') { j+=1; } } cout << ceil(k/j); return 0; }
15th Feb 2022, 1:29 PM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 2
Yes
16th Feb 2022, 7:47 AM
Arun Ruban SJ
Arun Ruban SJ - avatar
+ 1
Arun Ruban SJ So, I just need to count the letters in the string?
16th Feb 2022, 5:49 AM
adeadaccount