M1102-TD4 Exercie3 Corrigé

[Algo]
fonction TrouveCarDansStr (Caract : in caractere,
Chaine : in string,
Debut : in entier_naturel) renvoie entier_naturel
debut
declarer i : entier_naturel;
i <- Debut;
tant_que (i < taille(Chaine) ET_ALORS Caract ne_vaut_pas Chaine [i])
faire
i <- i +1;
ffaire
renvoie i;
fin

fonction ComptCarac (Caract : in caractere,
Chaine : in string) renvoie entier_naturel
debut
declarer Cpt : entier_naturel;
Cpt <- 0;

declarer Pos : entier_naturel;
Pos <- 0;

boucle
Pos <- TrouveCarDansStr (Caract, Chaine, Pos);
si (Pos vaut taille (Chaine)) sortie;
Cpt <- Cpt + 1;
Pos <- Pos + 1;
fboucle

renvoie Cpt;
fin

algorithme TestComptCarac
debut
declarer Str : string;
Str <- “Salut ca va?”;

afficher (“debut”);
afficher (“on trouve “, ComptCarac (‘a’, Str), ” fois la lettre ‘a’ dans “, Str);
ligne_suivante;
afficher (“on trouve “, ComptCarac (‘b’, Str), ” fois la lettre ‘b’ dans “, Str);
ligne_suivante;
fin
[/Algo]