+ 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
10 Antworten
+ 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.
+ 1
Ledexmar
can you give me an example of input and output, we will help you
+ 1
SoloProg nice, what's the difference between isblank and isspace?
0
Why are you decreasing the num inside the if statement?
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
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
0
Decreasing the num variable also affects the for loop.
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
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