The reason why you are getting this error message is because isspace()
accepts an integer value (int
) but operates on characters (typically of type char
). You must be passing an uninitialized negative value which is outside of the domain handled by isspace()
. The value passed is incorrect and the ispace()
implementation is gently informing you of this bug in your software. The library must have been compiled on a machine where there was an f:\
drive. The implementation does have more than 56 lines of code.
Also...
Use wordCount(strlen(wordArray), wordArray)
instead of passing SIZE
. Otherwise you will be reading uninitialized values which is bad.
Instead of run = false
use break
and replace the while with while(1)
. Also this loop very likely doesn't do what you think it does. You are merely overwriting the first byte with the second one. You probably want to shift all the characters to the left.
In your last loop, iterate until you hit a null byte ('\0'
) instead of up to size
as this is also incorrect. Note that the string may now be smaller than size
; perhaps size
should not be a parameter at all...
Given these problems, here is an alternative way to approach the problem which doesn't require modifying the original string.
int count_words(const char *s) {
int count = 0;
bool in_word = false;
while (*s != '\0') {
if (isspace(*s)) {
in_word = false;
}
else if (in_word == false) {
count += 1;
in_word = true;
}
++s;
}
return count;
}