Hi, question about if() in Average word Length - code coach | Sololearn: Learn to code for FREE!
New course! Every coder should learn Generative AI!
Try a free lesson
+ 1

Hi, question about if() in Average word Length - code coach

I want to discard dots and "?" in a parase so I used if (phrase== '.' || phrase== '?') and it seems that the tester sometimes don't recognize the '?' I solved the problem using 2(two) if, one for '.' and another for '?' Does somebody know why is it? I attached my code below https://code.sololearn.com/citb8SxMcHPK/?ref=app

2nd Aug 2022, 1:59 PM
Martin Ledezma
Martin Ledezma - avatar
10 Answers
+ 2
#include <iostream> #include <string> #include <cmath> using namespace std; int main() { string s; getline(cin, s); int w=1, t=0; for (size_t i=0; i<s.length(); i++) { if (isspace(s[i])) w++; if (isblank(s[i]) || ispunct(s[i])) continue; else t++; } double a = t/(double)w; cout << ceil(a) ; return 0; } https://code.sololearn.com/c4419b6zgpqR You could benefit from my solution.
2nd Aug 2022, 3:26 PM
SoloProg
SoloProg - avatar
+ 1
Ledexmar can you give me an example of input and output, we will help you
2nd Aug 2022, 3:04 PM
SoloProg
SoloProg - avatar
+ 1
SoloProg nice, what's the difference between isblank and isspace?
2nd Aug 2022, 3:31 PM
Martin Ledezma
Martin Ledezma - avatar
0
Why are you decreasing the num inside the if statement?
2nd Aug 2022, 2:46 PM
Lama
Lama - avatar
0
Tsering because it is counting every element, so whenever it finds a dot or '?' it will decrease that one. so It won't count them
2nd Aug 2022, 2:52 PM
Martin Ledezma
Martin Ledezma - avatar
0
I Have read that it is because inside the if () if the first condition is not met the next one after || wont be met either, whenever it finds a '?' it will check is it equal to '.'? because it is not, it won't check the next condition
2nd Aug 2022, 2:55 PM
Martin Ledezma
Martin Ledezma - avatar
0
Decreasing the num variable also affects the for loop.
2nd Aug 2022, 3:08 PM
Lama
Lama - avatar
0
it países all the tests if I just use an if for every condition, but it doesn't work if I use ||, the decresing is not the problem, because I only subtract the wrongly count
2nd Aug 2022, 3:14 PM
Martin Ledezma
Martin Ledezma - avatar
0
I don't know the test because is hidden, but it fails when I use if(phrase[i]=='.' || phrase[i]=='?') instead of two if
2nd Aug 2022, 3:16 PM
Martin Ledezma
Martin Ledezma - avatar