I want to count the number of spaces. As I understand it, compiler sweare at this line isspace(s[step]) != 0. But sometimes the code started and there was no error. I don't know why it is so.
char s[] = "So she was considering";
int number_space = 0, step = 0;
int length_string = strlen(s);
while(strlen(s) != step){
if(isspace(s[step]) != 0){
number_space++;
}
step++;
}
cout << number_space;
You have to write
or
Otherwise in general the expression s[step] can yield a negative value.
This code snippet
can be rewritten more simpler
That is there is no need to call
strlenand moreover in the condition of the loop.