1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42
| #include <stdio.h>
#include <string.h>
#define FORMAT "%1[01]"
size_t count (char const *chaine, char const *template);
int main (void)
{
char const *chaine = "111000111000";
char template[9] = { 0 };
int ret = 0;
do
{
puts ("Entrer la chaine a rechercher :");
ret = scanf (FORMAT FORMAT FORMAT FORMAT FORMAT FORMAT FORMAT FORMAT,
template, template + 1, template + 2, template + 3,
template + 4, template + 5, template + 6, template + 7);
scanf ("%*[^\n]"), getchar ();/* flush input buffer */
template[ret] = 0;/* don't forget that C strings end with nul byte */
}
while (ret < 3);
printf ("nombre d'occurences : %u\n", count (chaine, template));
return 0;
}
size_t count (char const *chaine, char const *template)
{
char *match = NULL;
size_t nb_match = 0;
size_t template_len = strlen (template);
while ((match = strstr (chaine, template)) != NULL)
{
nb_match++;
chaine = match + template_len;
}
return nb_match;
} |
Partager