Docsity

String Manipulation Functions in Pascal, Cheat Sheet of Mathematical Analysis

Toggle sidebar
bg1
Série 5 : Les chaînes de caractères
Page 1 sur 3
Ecrire les fonctions suivantes (même si elles sont fournies par le compilateur) :
1 – calcul de la longueur d’une chaîne
function langeur(str:string):
integer; var i: integer;
begin
i := 0;
while str[i+1] <> #0 do
i:=i+1;
langeur:=i;
end;
2 – concaténer deux chaînes
function concatenation( str1,str2: string): string;
begin
concatenation:=str1+str2;
end;
3 – Vérifier si une sous chaîne existe dans une chaîne
function sousChaineExiste(chaine,sousChaine:string):boolean;
var i,j:integer;
existe,found:boolean;
begin
found := false;
i:=1;
while (i<=Length(chaine)-Length(sousChaine)+1) and (found=false) do
begin
if chaine[i]=sousChaine[1] then
begin
j:=2;
existe:=true;
while(j<=Length(sousChaine)) and (existe=true) do
if chaine[i+j-1] <> sousChaine[j] then
existe := false
else j:=j+1;
end;
found:=existe;
i:=i+1;
end;
SousChaineExiste := found;
end;
4 – calculer le nombre d’occurrences d’une sous chaîne dans une chaîne
function NombreOccurrences(sousChaine, chaine: string):integer;
var i, j, compteur: integer;
begin
compteur := 0;
for i := 1 to length(chaine) - length(sousChaine) + 1 do
begin
j := 1;
while (j <= length(sousChaine)) and (chaine[i + j - 1] = sousChaine[j]) do
j:=j+1;
if j > length(sousChaine) then
compteur:=compteur+1;
end;
NombreOccurrences := compteur;
end;
pf3

Partial preview of the text

Download String Manipulation Functions in Pascal and more Cheat Sheet Mathematical Analysis in PDF only on Docsity!

Ecrire les fonctions suivantes (même si elles sont fournies par le compilateur) :

1 – calcul de la longueur d’une chaîne

function langeur(str:string): integer; var i: integer; begin i := 0; while str[i+1] <> #0 do i:=i+1; langeur:=i; end;

2 – concaténer deux chaînes

function concatenation( str1,str2: string): string; begin concatenation:=str1+str2; end;

3 – Vérifier si une sous chaîne existe dans une chaîne

function sousChaineExiste(chaine,sousChaine:string):boolean; var i,j:integer; existe,found:boolean; begin found := false; i:=1; while (i<=Length(chaine)-Length(sousChaine)+1) and (found=false) do begin if chaine[i]=sousChaine[1] then begin j:=2; existe:=true; while(j<=Length(sousChaine)) and (existe=true) do if chaine[i+j-1] <> sousChaine[j] then existe := false else j:=j+1; end; found:=existe; i:=i+1; end; SousChaineExiste := found; end;

4 – calculer le nombre d’occurrences d’une sous chaîne dans une chaîne

function NombreOccurrences(sousChaine, chaine: string):integer; var i, j, compteur: integer; begin compteur := 0; for i := 1 to length(chaine) - length(sousChaine) + 1 do begin j := 1; while (j <= length(sousChaine)) and (chaine[i + j - 1] = sousChaine[j]) do j:=j+1; if j > length(sousChaine) then compteur:=compteur+1; end; NombreOccurrences := compteur; end;

5 – remplacer une sous chaîne par une autre(devoir de maison) (nvl chaine)

function remplace (chaine,schaine,nschaine:string):string; var Resultat: string; i,j,n: integer; found: boolean; begin Resultat := ''; i := 1; while i <= Length(chaine) do begin if chaine[i] = schaine[1] then begin found := true; j := 2; while (j <=Length(schaine)) and (j+i- 1 <=Length(chaine)) and (found=true) do if chaine[j+i-1] <> schaine[j]then found := false else j := j + 1; if found and (j+i-1 <= Length(chaine)) then begin Resultat := Resultat + nschaine; i := i + Length(schaine); end else begin Resultat := Resultat + chaine[i]; i := i + 1; end; end else begin Resultat := Resultat + chaine[i]; i := i + 1; end; end; remplace:= Resultat; end;