I know that strchr, searches for the first occurences of a char in a string... I just wanted know if I can use this code to count characters in a string as example.
String: An apple a day keeps the doctor away
Char to search: a
Result: Found a 6 times in String.
1. I have figured out something like this.
Code:
#include <string.h>
#include <stdio.h>
int main(void)
{
char string[15];
char *ptr, c = 'i';
int i;
strcpy(string, "This is a string");
ptr = strchr(string, c);
if (ptr)
while(ptr)
{
ptr = strchr(string, NULL);
i++;
}
printf("Char %c, found %d times in string", c, i);
else
printf("Char was not found in string");
}
But this code didn't work... so I just need a bugfix in this code could you help me out a little bit ???
2. How do I get this code to find also the Uppercase and the Lowercase 'A' in this code ?
Thanks in advance
Client