+ 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; }
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;
}
+ 2
Yes
+ 1
Arun Ruban SJ So, I just need to count the letters in the string?