1. //extragere cuvinte cu mai multe spatii si alte caractere



Yüklə 32,28 Kb.
tarix13.11.2017
ölçüsü32,28 Kb.

Probleme rezolvate stringuri
1. //extragere cuvinte cu mai multe spatii si alte caractere

#include

#include

#include

void main()

{ char sir[200],cuv[200],*p,sep[]=",!? "; int i,j;

clrscr();

cin.get(sir,100);

i=1;

while(i

{

if( strchr(sep,sir[i])!=NULL && strchr(sep,sir[i-1])==NULL)

{ strxfrm(cuv,sir,i+1);

//prel cuvant

strcpy(sir,sir+i);//sterg

cout<

//cout<

i=0;


while( strchr(sep,sir[i])!=NULL) strcpy(sir,sir+1);

i=1;}


else i++;

}

cout<

//prelucreaza ultimul cuvant ramas in sir

}

//extragere cuvinte cu mai multe spatii sau alte caractere

#include

#include

#include

void main()

{ char sir[200],cuv[200],*p; int i,j;

clrscr();

cin.get(sir,100);

p=strchr(sir,' ');

while(p)

{ strxfrm(cuv,sir,p-sir+1);

//prelucrare cuvant

strcpy(sir,p);int poz=p-sir;

while(sir[poz]==' ') strcpy(sir,sir+1);

p=strchr(sir,' ');

cout<

cout<

}

//prelucreaza ultimul cuvant ramas in sir



}

2. /* un text de maxim 200 caractere, cuvinte separate printr-un singur spatiu; sa se afiseze cuvintele care

contin cel mult 3 vocale */

#include

#include

#include

#include

void main()

{

clrscr();



char sir[255],cuv[255],voc[5]="aeiou",*p,*q;int i,n;char c[1]=" ";

cin.get(sir,255); cin.get();

/* n=strlen(sir); */

p=strchr(sir,c[0]);

while (p)

{ cuv[0]=0;

/* ptr initializare sir vid cuv[0]=NULL sau cuv[0]='\0' */

strncpy(cuv,sir,p-sir);

//Atentie sirul cuv nu este terminat cu NULL

int k=0; cuv[p-sir]=0;

n=strlen(cuv);

for(i=0;i

{ if (strchr(voc,cuv[i])) k++;}

if( k<=3) cout<

strcpy(sir,p+1);/* sterg primul cuvant din sir */

cout<

p=strchr(sir,c[0]);

}

n=strlen(sir); /* ultimul cuvant ramas in text */



int k=0;

for(i=0;i

{ if (strchr(voc,sir[i])) k++;}

if( k<=3) cout<

}
Exemplu

#include

#include

#include

void main()

{

clrscr();



int i=0; char x[1002];

fstream f("fis.txt",ios::out);

for (i=1;i<=1000;i++)

{ f<<'a';}

f.close();

fstream f1("fis.txt",ios::in);



f1.getline(x,1001);

f1.close();

cout<

}

3. prefixe

#include

#include

#include

void main()

{

clrscr();



char sir[100],prefix[100]; int i;

cout<<"cuvantul initial=";

cin.get(sir,50);

for(i=0;i

{ strxfrm(prefix,sir,i+2);

cout<



}

}
4. sufixe

#include

#include

#include

void main()

{

clrscr();



char sir[100],sufix[100]; int i;

cout<<"cuvantul initial=";

cin.get(sir,50);

for(i=0;i<=strlen(sir);i++)

{ strcpy(sufix,sir+strlen(sir)-i);

cout<

}

}
5. /* sa se stearga toate vocalele



dintr-un cuvant in ordinea in care apar

Ex: "examen" xamen, xmen, xmn */

#include

#include

#include

void main()

{ clrscr();

char sir[255],voc[5]="aeiou",*p,*q;int i,n;

cin.get(sir,20);

n=strlen(sir);

i=0;


while (i{ p=strchr(voc,sir[i]);/* p=pointer pentru voc, q pointer pentru sir*/

if (p) {q=&sir[i];strcpy(q,q+1);cout<

else i++;

}

}


6. /* fie sir="teuuuesppi", sa se stearga toate caracterele identice aflate pe pozitii succesive

prima data va sterge uuu=> "teesppi"

=>tsppi =>tsi... */
#include

# include

# include

void main()

{

clrscr();



char sir[255],*p,*q,*r;

cin.get(sir,255);

cin.get();

p=q=r=sir;

cout<

int lung=strlen(sir);

int i=1;

while (i

{

int ok=0;p=&sir[i-1];q=&sir[i];int j=i;



while (sir[j]==sir[i-1])

{ j++;lung--;ok=1;q++;}

if(ok) {strcpy(p,q);i--;}

else i++;

}

cout<

}
7. stergere….
#include

#include

#include

void main()

{ char sir[200],final[200],c[30],cs[30],temp[100],*p; int i,j,l,nr;

clrscr();

cin.get(sir,100);

cin.get();

cin.get(c,25);

l=strlen(c);

final[0]=NULL;

for(i=0;i

cs[i]='*';

cs[l]=NULL;

p=strstr(sir,c);

while(p)


{

strxfrm(temp,sir,p-sir);

strcat(final,temp);

strcat(final,cs);

nr=p-sir+l;

strcpy(sir,sir+nr);

p=strstr(sir,c);

}

strcat(final,sir);



cout<
8. /* sa se stearga toate vocalele

dintr-un cuvant in ordine alfabetica

Ex: "bacalaureat"

se va sterge a=> bcluret

e=>bclurt...*/

#include

#include

#include

void main()

{ clrscr();

char sir[255],voc[5]="aeiou",*p;int i;

cin.get(sir,20);

for(i=0;i<5;i++)

{

p=strchr(sir,voc[i]);int ok=0;



while(p)

{ strcpy(p,p+1);p=strchr(sir,voc[i]);ok=1;}

if (ok) cout<

}

}


9. test-cin

#include

# include

# include

void main()

{

clrscr();



char *p,*q; char sir1[255],sir2[255];

int i;


/* p="ana are mere";

cout<



p="test";

cout<



q=++p;

cout<

cout<


cin.get(sir1,255);

cin.get();

cin.get(sir2,255);

cout<

cin>>sir1; /* retine primele caractere pana la spatiu*/

cout<

}

V7- sa se elimine ultima consoana dintr-un text
#include

#include

#include

void main()

{

clrscr();



char fraza[255]; int i;

cout<<"fraza =";

cin.get(fraza,250);

int ok=1;

i=strlen(fraza)-1;

while (i>0&& ok==1)

{ if(strchr("aeiou",fraza[i])==NULL &&fraza[i]!=' ')

{ strcpy(fraza+i,fraza+i+1);

ok=0;}

else i--;



}

cout<

}
V28

#include

#include

#include

void main()

{ clrscr();

char cuv[21],voc[]="aeiou",*p,clona[21]; int i;

cout<<"cuvant =";

cin.get(cuv,20);

for(i=0;i<5;i++)

{ strcpy(clona,cuv);

p=strchr(clona,voc[i]);int ok=0;

while(p)

{ strcpy(p,p+1);

p=strchr(clona,voc[i]);

ok=1;}


if(ok) cout<
}



Dostları ilə paylaş:


Verilənlər bazası müəlliflik hüququ ilə müdafiə olunur ©genderi.org 2019
rəhbərliyinə müraciət

    Ana səhifə