Add count_chars

This commit is contained in:
flyingscorpio@pinebookpro 2021-11-29 09:17:59 +01:00
parent ded7b58971
commit 13fbf18269
2 changed files with 39 additions and 0 deletions

1
.gitignore vendored
View file

@ -42,3 +42,4 @@ calcul-somme-tableau
puissance
is_a_number
val_presente_ou_non
count_chars

View file

@ -0,0 +1,38 @@
/* Count chars */
#include <stdio.h>
#include <stdlib.h>
int count_chars(char, char*);
int main() {
char *chaine = "Hello world!";
char caractere;
int count;
caractere = 'e';
count = count_chars(caractere, chaine);
printf("Chaine: %s\n", chaine);
printf("Le caractère '%c' est présent %d fois.\n", caractere, count);
caractere = 'l';
count = count_chars(caractere, chaine);
printf("Chaine: %s\n", chaine);
printf("Le caractère '%c' est présent %d fois.\n", caractere, count);
return 0;
}
int count_chars(char caractere, char *chaine) {
int i = 0;
int count = 0;
while (chaine[i] != '\0') {
if (chaine[i] == caractere) {
count++;
}
i++;
}
return count;
}