I am working a coding problem in my book about using the strchr() function to search for a char in a string. Now, I can obviously do that, but the problem I am having is it wants me to now have the program count how many times that certain characters appears in the string. The problem is that since strchr goes until the first occurence and stops; I am not sure as to what to do next... I was thinking a for loop, but I can't come up with a correct loop. Here is my code:
Now, would a for loop be put to good use here or am I thinking off?
const char *string = "The bad man waned throughout the day";
char character = 'a';
int count = 0;
if(strchr(string, character) != NULL)
printf("Character %c appears %d times\n", character, count);
Sorry I didn't add this in the main post, but I am searching for the character a as an example so I can get it to work.