count occurrences in string C

Hi guys

I have

#include <stdio.h>
#include <ctype.h>
#include <string.h>

count()
{
int i,found=0;
char string[30];

printf("Type a string : ");

gets(string);

for(i=0;i<strlen(string);i++)

found++;
printf("Number of characters: %d\n",found);

}
main()
{
count();
}

which counts the length of the string - i`m just new to strings
my objective is to Count the number of occurrences of a word or letter in a string

example : i have char string1[30] ="addaddaddadd";
and char c="a"; using char c="a"; i want to count how many a does string1[30];
has or have..
}
sorry for my grammar ..
any help is appreciated , thanks
Last edited on
1. you just need to modify:
1
2
for(i = 0; i < strlen(string); i++)
    found++;

to test whether string[0]...string[n] == 'a'

additionally:
2. modern C/++ requires a return type for functions
3. never use gets() (or scanf()), use fgets() instead, read: http://www.gidnetwork.com/b-56.html
thanks matsom
Topic archived. No new replies allowed.