+ 2
Avg word length problems - Help //SOLVED
Need help with a Code Coach challenge, got 4/5 answers right except the first. Help. https://code.sololearn.com/c3kcrIXV58dw/?ref=app
4 ответов
+ 1
Post your exact code which you are using in code coach...
This comparison will not work in c.
if(str[i] == "?.,!"){
str[I] is charecter and your comparing with string.. Not works.. Use a loop to check if str[i] is a in a list punctuations or use ctype.h methods isalpha(str[i]);
+ 1
Jayakrishna🇮🇳
here Is what Is In code coach, I knew It wasn’t right but somehow 4/5 were correct. Gonna try Isalpha now though. Thank you.
/*
Average Word Calculator - Medium code Coach challenge in C
first solution is the only incorrect answer. Help!
*/
#include <stdio.h>
#include <string.h>
#include <math.h>
int main() {
char str[100];
//difference from char str[] = " " ?
int i, avg, count = 0;
double words, letters = 0;
fgets(str, 100, stdin);
//gets wouldn't work??
//printf("%s", str); test
for(i = 0; i < strlen(str); i++){
if(str[i] == ' '){
count++;
}else if(str[i] == "?.,!"){
}else{
letters++;
}
}
words = count + 1;
avg = ceil((letters/words));
printf("%d", avg);
//printf("%f", words); 5 is correct
//printf("%f", letters); 16 is wrong; counting puncuation characters??
return 0;
}
+ 1
Jayakrishna🇮🇳 I haven’t changed it yet
0
Roderick Davis where you used ispha. You still posted same for str[i] =='*&#v' you cannot compare charecter with string...
Use this
else if (isalpha(str[i]))
letters++;
But also space is also a charecter so you need to increment letters++ for space..
Add ctype.h remaining with all works...
Edit:
if(str[i] == ' '){
count++; letters++;
}else if(isalpha(str[i])){
letters++;
}
}