Sunteți pe pagina 1din 8

Siruridecaracterestring

Unsirdecaracterereprezintaosuccesiunedecaracterecuprinsaintre
apostrofuri.Aceastasuccesiunepoatefialcatuitadin:
literemici;
literemari;
cifre;
delimitatori(.,;);
caracterespeciale(#@);
UnsirdecaracterepoatefimemoratintrovariabiladetipSTRING.
Declarareauneiastfeldevariabilesepoaterealizefieprinspecificareadimensiunii
maximeasirului,fienu.
1.varnume:string[25];2.varnume:string;
nume:string[nr];1<=nr<=255;
Dacaladeclarareauneivariabileestespecificatadimensiuneamaximaasirului
decaractere,atuncicompilatorulvaalocaunnumarnrdecaracterepentruaceasta
variabila.
Dacadimensiuneanuestespecificataatuncicompilatorulvaalocadimensiunea
maxima,adica255decaractere.
Unuisirisepoateasocialovaloareprindouamodalitati:
a)prinatribuireadirectaauneivalorifolosindoperatoruldeatribuire(:=);
b)prinprocedura(read,readln);
!!!Observatie!!!
Dacavaloareaceiseatribuieunuisirdecaracteredepasestelungimeamaximaa
sirului,atuncisiruldecaracterevaprimiovaloaretrunchiatadinvaloareainitiala
egalaculungimeamaximaasa.
Exemple:
a)s:string[3];s:=abcde=>s=abc;
b)s:string;s:=abcde=>s=abcde;
c)s=stringuldelungime0;
d)s=sirdecaracteredelungime1.

Operatiipesiruridecaractere
1)Concatenareaadouasaumaimultesiruridecaractere.
Serealizeazafolosindoperatorul+.
s:string;
s:=ab++cde;
s=abcde
2)Comparareaadouacaractereprecumsicomparareaadouasiruridecaracterese
realizeazafolosindoperatoriirelationali:=,<,<=,>,>=,<>.
ComparareaadouacaractereserealizeazacomparandcodurileAscialecelordoua
caractere.
Comparareaadouasiruridecaractereserealizeazaprincompararealexicografica,
reducanduselacomparareaadouacaractere.
3)Parcurgereaunuisirdecaractere
length(sir)returneazalungimeasiruluispecificat.
sir:string;
Valoareareturnatadeaceastafunctieesteovaloaredetipbyte.
length()=0sirulvid.
s:string;
s[i]caracteruldepepozitiai;
fori:=1tolength(s)do
<prelucreazas[i]>;
!!!Observatii!!!
1)Relatiadelegaturaintrecaracterelemarisicaracterelemiciesteurmatoarea:
CA(mare)+32=CA(mic);CAcodulAsci;
2)ord,chrdouafunctiipredefinitespecificecaracterelor;
ORD(uncharacter)=codulAscialcaracterului;
CHR(unnumar)=caracterulcorespunzatorcoduluiAsci.

Functiipredefinitepestringuri
1)POS(sir1,sir2);
2)COPY(sir,poz,nr);
3)LENGTH(sir);
4)CONCAT(c1,c2,,cn);
1)FunctiaPOSverificadacasirul1sesitueazainsirul2sireturneazapozitiade
inceputinsirul2dacaexistasau0dacasirul1nuseaflainsirul2.
2)FunctiaCOPYextrageunsubsiralsiruluispecificat,incepanddinpozitiapozcu
unnumarnrdecaractere.
3)FunctiaLENGTHreturneazalungimeaunuisirdecaractere.

4)FunctiaCONCATlipestemaimultesiruric1,c2,,cn.
!!!Observatii!!!
1)Dacapozestemaimaredecatlength(sir)atuncisirulrezultatvafisirulvid.
2)Dacanrestemaimaredecatlength(sir)poz+1atuncisubsirulobtinutvacontine
numailength(sir)poz+1caractere.
|||
1pozlength(sir)
||
Length(sir)poz+1

Proceduripredefiniteinstringuri
1)DELETE(sir,poz,nr)
Stergedinsirunsirincepandcupozitiapozunnumarnrdecaractere.
!!!Observatie!!!
1)Dacapoz>length(sir)atuncinusestergenimic.
2)Dacanr>length(sir)poz+1,atuncisevorstergelength(sir)poz+1caractere.
Exemple
S=casa;
1)delete(S,2,2)>S=ca;
2)delete(S,7,3)>S=casa;
3)delete(S,3,7)>S=ca.
2)INSERT(subsir,sir,poz)
Insereazasubsiruldatinsirulsirincepanddinpozitiapoz.
!!!Observatii!!!
1)Dacapozitiaestemaimaredecatlength(sir),atuncisubsirulseinsereazala
sfarsitulsirului.
subsir=rr;sir=casa;
insert(subsir,sir,30)>sir=casarr;
insert(subsir,sir,2)>sir=crrasa.
2)Dacalungimeasirului+lungimeasubsiruluidepasestelungimeadedefinitiea
siruluisir,atuncilungimeasiruluisepastreazalalungimeadedefinitie
(trunk(int))characterdedupalungime.
sir=casa;subsir=rr;sir:string[4];
insert(subsir,sir,2)>sir=crra.
3)STR(numar,sir)
Transformavariabilanumarintrunsirdecaractere.

12>12.
4)VAL(sir,numar,eroare)
Incearcasatransformesiruldecaracteresirintrunnumarreal.Dacareuseste
atunciparametruleroarevaaveavaloarea0,dacanureusestevaloarea
parametruluinumar=0,eroarevaaveapozitiadeundeincepenereusita.
Val(12.3,numar,eroare)>eroare=0
>numar=12.3
Val(12,3,numar,eroare)>eroare=3
>numar=0.

SETDEPROBLME
1)Saseafisezetoateprefixeleunuicuvant.
2)Saseafisezetoatesufixeleunuicuvant.
3)Saseeliminetoatespatiiledintruntext.
4)Verificatidacauncuvantestepalindrome.5)Afisaticatevocalecontineuntext.
6)Sedauntextincarecuvintelesuntseparateprinspatii.Saseafisezepecateun
randcuvinteletextului.
Programproblema_1;
Varcuv:string;
n,i:integer;
Begin
write(Cuvantuleste:);
readln(cuv);
n:=length(cuv);
fori:=1tondo
writeln(copy(cuv,1,i);
readln;
END.

Programproblema_2;
Varcuv:string;
n,i:integer;
Begin
write(Cuvantuleste:);
readln(cuv);

n:=length(cuv);
fori:=ndownto1do
writeln(copy(cuv,i,n);
readln;
END.

Programproblema_3;
Varcuv:string
i,n:integer;
Begin
write(Cuvantuleste:);
readln(cuv);
n:=length(cuv);
fori:=1tondo
ifcuv[i]=thenbegin
delete(cuv,i,1);
n:=n1;
end;
fori:=1tondo
write(cuv[i]);
readln;
END.

Programproblema_4;
Varcuv:string;
i,n:integer;
OK:Boolean;
Begin
write(Cuvantuleste:);
readln(cuv);
n:=length(cuv);
OK:=true;
fori:=1tondiv2do
ifcuv[i]<>cuv[ni+1]thenOK:=false;
ifOKthenwriteln(Cuvantulestepalindrom)
elsewriteln(Cuvantulnuestepalindrom);
readln;
END>

Programproblema_5;
Vartext:string;
i,n,nr:integer;
Begin
write(Textuleste:);
readln(text);
n:=length(text);
nr:=0;
fori:=1tondo
if(text[i]in[A,E,I,O,U])or(text[i]in[a,e,i,o,u])theninc(nr);
writeln(sunt,nr,vocale);
readln;
END.

Programproblema_6;
Vart:string;
n,i:integer;
cuv:string;
Begin
Write(Datitextul:);
Readln(t);
N:=length(t);
I:=1;
Whilei<=ndo
Begin
While(t[i]in[,,,.,?,!,:,;])and(i<=n)do
Inc(i);
Cuv:=;
Whilenot(t[i]in[,,,.,?,!,:,;])and(i<=n)do
Begin
Cuv:=cuv+t[i];
Inc(i);
End;
Writeln(cuv);
End;
Readln;
End.

Fiiere
Unfiieresteostructurdedatecucomponentenumitenregistraricepotavea
dimensiunifixesauvariabile,inceldealdoileatrebuindsaexistenite
marcatoricaresledelimiteze.
Fisierelesepotclasificadupamaimultecriterii:

dupaaccesullacomponente:

fiierecuaccessecvenialincareprelucrareadatelorsefacenordineancare
acesteaaufostcitite;
fiierecuaccesdirectincareaccesullacomponentesefacenmoddirect,fara
aseinecontdeordineaintroduceriilor;
dupconinutulfiierelor:
fiieretext,datelefiindconsideratecaracterescrisesecvenial,uneledup
altele,pelinii,asemntormoduluinaturaldescriere;
fiierebinare,cepotconinedatedetipuridiferite,nacestcazfiecare
componentesteconsiderat1octet ;

Operaiicufiieretext

Unfiiertextesteunfiieralecruicomponentesuntcaracterescrise
secvenialpelinii.Pentruaputealucracufiieretexttrebuiecantrunprogram
Pascalsavemdefinitovariabilcaresfaclegturacuunfiiertext
memoratpeunsuportextern(HDD).Oastfeldevariabilsedeclar:
nume_variabil:text;
Exemplu:f:text;
Legturadintreovariabilifiierulexternsefacecuajutorul
proceduriiassign:
assign(variabil_fiier,'nume_fiier_extern');
Exemplu:assign(f,'fiier.txt');

Alteproceduridelucrucufiieretextsunt:

rewriteesteoprocedurcepermitecreeareaunuifiiertextideschiderea
acestuiapentruscriereanel.Easepoateutilizaastfel:rewrite(f);,undefeste
variabiladetipfiiertextasociatcuunfiiertextextern;
resetprocedurcepermitedeschidereaunuifiierpentrucitireadinacesta;
appendprocedurcepermitescrierealasfritulfiieruluitext,adic
adugareauneinoiinformaiilaceaexistent;
closeprocedurcuajutorulcreiasenchideunfiiertext