I know strchr only gets the first instance. But why is my loop not working. No matter how many times '.' or ' !' or '?' is used in the text. It keeps outputting 3.
//this finds number of sentences
int Y = 0;
while (ch != '\0');
if(strchr(text,'.') != NULL)
Y++;
if(strchr(text,'!') != NULL)
Y++;
if(strchr(text,'?') != NULL)
Y++;
For starters it seems there is a typo
In any case this code snippet
does not make sense because the search of target characters always start from the beginning of
text.The function
strchris not an appropriate function for this task if to use separate calls of the function for each target symbol.In this case using this function you need three separate loops one for each target symbols.
Here is a demonstrative program that shows how the task can be performed using another string function
strcspn.The program output is