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:
Code:
#include <stdio.h>
#include <string.h>
void main()
{
const char *string = "The bad man waned throughout the day";
char character = 'a';
int count = 0;
if(strchr(string, character) != NULL)
count++;
printf("Character %c appears %d times\n", character, count);
}
Now, would a for loop be put to good use here or am I thinking off?
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.