Add count_chars
This commit is contained in:
parent
ded7b58971
commit
13fbf18269
2 changed files with 39 additions and 0 deletions
1
.gitignore
vendored
1
.gitignore
vendored
|
@ -42,3 +42,4 @@ calcul-somme-tableau
|
|||
puissance
|
||||
is_a_number
|
||||
val_presente_ou_non
|
||||
count_chars
|
||||
|
|
38
programmation-c-cpp/tp5/count_chars.c
Normal file
38
programmation-c-cpp/tp5/count_chars.c
Normal 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;
|
||||
}
|
Loading…
Reference in a new issue