Algorithme occurence juste ?
capa57
Messages postés
383
Statut
Membre
-
capa57 Messages postés 383 Statut Membre -
capa57 Messages postés 383 Statut Membre -
Bonjour,
j'ai fait un algo j'aimerai savoir si c'est juste
il doit permettre de compter le nombre d'occurences d'une chaine dans une autre chaine
exemple : ABC dans XABCGGABCJJJAB ; on aura pour résultat : 2
Algo occurence
Var
chaineMot, chaineRech : chaine
nbLettre, pos, y : entier
absent : booléen
Début
Afficher(" saisir le mot entier")
saisir (chaineMot)
Afficher ("saisir la chaine a rechercher")
saisir (chaineRech)
nbLettre <-- Longueur (mot)
y <-- 0
absent <-- rang (chaineMot, chaineRech , 1) = 0
pos <-- rang (chaineMot, chaineRech , 1) + 1
Tant que ( NON absent et pos =< nbLettre)
y <-- y + 1
absent <-- rang (chaineMot, chaineRech , pos) = 0
pos <-- rang (chaineMot, chaineRech , pos) + 1
FTQ
Si NON absent
alors y <-- y + 1
Fsi
Afficher (" il y a " + y + " occurence " )
Fin
j'ai fait un algo j'aimerai savoir si c'est juste
il doit permettre de compter le nombre d'occurences d'une chaine dans une autre chaine
exemple : ABC dans XABCGGABCJJJAB ; on aura pour résultat : 2
Algo occurence
Var
chaineMot, chaineRech : chaine
nbLettre, pos, y : entier
absent : booléen
Début
Afficher(" saisir le mot entier")
saisir (chaineMot)
Afficher ("saisir la chaine a rechercher")
saisir (chaineRech)
nbLettre <-- Longueur (mot)
y <-- 0
absent <-- rang (chaineMot, chaineRech , 1) = 0
pos <-- rang (chaineMot, chaineRech , 1) + 1
Tant que ( NON absent et pos =< nbLettre)
y <-- y + 1
absent <-- rang (chaineMot, chaineRech , pos) = 0
pos <-- rang (chaineMot, chaineRech , pos) + 1
FTQ
Si NON absent
alors y <-- y + 1
Fsi
Afficher (" il y a " + y + " occurence " )
Fin
2 réponses
-
la fonction rang sert à retourner la position d'un caractère dans une chaine
exemple
rang (chaineMot, chaineRech , 1)
on recherche "chaineRech" dans "chaineMot" à partir de la "1" ère lettre -
Bonjour,
Je ne comprend pas la fonction de rang() car il prend les deux chaines et une position on ne sait pas de quelle position il s'agit; vous devriez pouvoir traduire cet algo avec le vôtre :
algo_occurence chainemot, chainerech : chaine nb_chainemot, nb_chainerech, y, x, cpt : entier nb_chainemot <-- longueur(chainemot) nb_chainerech <-- longueur(chainerech) y <-- 0 x <-- 0 cpt <-- 0 TANTQUE y < nb_chainemot FAIRE SI caractere(chainemot,y) = caractere(chainerech,x) ALORS x <-- x + 1 FINSI SINON x <-- 0 SI x = nb_chainerech ALORS cpt <-- cpt + 1 x <-- 0 FINSI y <-- y + 1 FINTANTQUE RETOURNE cpt