char2

Màu nền
Font chữ
Font size
Chiều cao dòng

//đếm số lần xuất hiện của chuỗi b trong chuỗi a

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

int demtu(char *a,char *b)

{int d=0;

while(strstr(a,b)!=NULL)

{d++;

a=strstr(a,b)+1;}

cout<<"ki tu "<<b<<" xuat hien "<<d<<" lan";

}

void main()

{

clrscr();

char a[100],b[100];

cout<<"nhap chuoi a:";cin.ignore(1);cin.get(a,100);

cout<<"nhap chuoi b:";cin.ignore(1);cin.get(b,100);

demtu(a,b);

getch();

}

// tìm tên chuỗi b trong a

#include<iostream.h>

#include<conio.h>

#include<string.h>

#include<ctype.h>

void timten(char *a,char *b)

{

int n=strlen(a);

char *s;

for(int i=n-1;i>=0;i--)

if(a[i]==' ')

{s=a+i+1;break;           }

if(strcmpi(s,b)==0)// strcmp==stricmp

cout<<s;

else

cout<<"ban nhap sai ten

";

}

void main()

{

clrscr();

char a[100],b[100];

cout<<"nhap chuoi a:";cin.ignore(1);cin.get(a,100);

cout<<"nhap chuoi b:";cin.ignore(1);cin.get(b,100);

timten(a,b);

getch();

}

//xóa 1 kí tự cua chuỗi a với kí tự nhập từ phím

void xoakitu(char *a,char b)

{

int n=strlen(a);

for(int i=1;i<n;i++)

if(a[i]==b)

{

for(int j=i;j<n;j++)

a[j]=a[j+1];

i--;n--;

}

cout<<a;

}

void main()

{

clrscr();

char a[100],b;

cout<<"nhap chuoi a:";cin.get(a,100);

cout<<"nhap chuoi b:";cin.ignore(1);cin.get(b);

xoakitu(a,b);

getch();

}

//xóa 1 từ trong chuỗi (từ được nhập từ phím)

void xoatu(char *s,char *c)

{

while(strstr(s,c)!=NULL)

{

int t=strlen(s)-strlen(strstr(s,c)),d=strlen(c);

if((s[t-1]==' '&& s[t+d]==' ')||(s[t-1]==' '&& t+d==strlen(s)))

strcpy(s+t-1,s+t+d);// s+t-1 la khoang trang

if(s[t+d]==' '&& t==0)//truong hop xoa tu dau tien

strcpy(s,s+t+d+1);

}

cout<<s;

}

void main()

{

clrscr();

char s[100],c[100];

cout<<"nhap chuoi s:";cin.get(s,100);

cout<<"nhap chuoi c:";cin.ignore(1);cin.get(c,100);

xoatu(s,c);

getch();

}

// viết hoa chữ cái đầu mỗi từ trong chuỗi

void chuhoa(char *s)

{

s[0]=(s[0])-32;

for(int i=1;i<strlen(s);i++) //chua tim dc cach nao toi uu hon

if(s[i]==' ')

{s[i+1]=toupper(s[i+1]);

i++;}

else

{

if(s[i]>='A' && s[i]<='Z')

s[i]=s[i]+32;

}

cout<<s;

}

void main()

{

clrscr();

char s[100];

cout<<"nhap chuoi s:";cin.get(s,100);

chuhoa(s);

getch();

}

//xóa chuỗi từ vị trí và số kí tự xóa nhập từ phím

void xoa(char *s,int vt,int n)

{strcpy(s+vt,s+vt+n);

cout<<s;}

void main()

{

clrscr();

char s[100];

int vt,n;

cout<<"nhap chuoi s:";cin.get(s,100);

cout<<"nhap vi tri bat dau xoa:";cin>>vt;

cout<<"nhap so ki tu can xoa:";cin>>n;

xoa(s,vt,n);

getch();

}

// lọc chữ cái xuất hiện nhiều nhất trong 1 chuỗi

void xuathiennhieunhat(char *s)

{

int a[100],n=0;

for(int i=0;i<(int)strlen(s);i++)

{int d=1;

for(int j=i+1;j<(int)strlen(s);j++)

if(s[i]==s[j])

{d++;

for(int k=j;k<(int)strlen(s);k++)

s[k]=s[k+1];

j--;

}

a[n++]=d;}

int max=a[0];

for(i=1;i<(int)strlen(s);i++)

if(a[i]>max)

max=a[i];

for(i=1;i<(int)strlen(s);i++)

if(a[i]==max)

cout<<"ki tu "<<s[i]<<" xuat hien nhieu nhat la "<<a[i]<<" lan

";

}

void main()

{

clrscr();

char s[100];

cout<<"nhap chuoi s:";cin.get(s,100);

xuathiennhieunhat(s);

getch();

}

// đếm tần suất xuất hiện từng chữ cái trong chuỗi.

void demkitu(char *s)

{int a[100],n=0;

for(int i=0;i<(int)strlen(s);i++)

{int d=1;

for(int j=i+1;j<(int)strlen(s);j++)

if(s[i]==s[j])

{d++;

            for(int k=j;k<(int)strlen(s);k++)

            s[k]=s[k+1];

            j--;}

a[n++]=d;

}

for(i=0;i<(int)strlen(s);i++)

cout<<"ki tu "<<s[i]<<" xuat hien "<<a[i]<<" lan

";

}

void main()

{

clrscr();

char s[100];

cout<<"nhap chuoi s:";cin.get(s,100);

demkitu(s);

getch();}

//đảo chuỗi và sao chép chuỗi mới với vị trí và số lượng kí tự nhập từ phím

void copy(char s[], int vt, int sl)

{

 strcpy(s+vt-1,s+vt+sl-1);

 cout<<" chuoi sau khi copy la "<<s;

}

void main()

{

clrscr();

char a[100];

char b[100];

int A,B;

cout<<" nhap moi mot chuoi a =";

cin.get(a,100);

A=strlen(a);

B=strlen(b);

cout<<" chuoi dao nguoc cua chuoi s = ";

for(int i=A; i>=0; i--)

{

cout<<a[i];

}

// tao mot chuoi moi voi vt tri nhap vao va so luong ky tu lay

int vt;

int sl;

cout<<"

nhap vao vi tri can sao chep vt ="; cin>>vt;

cout<<"

nhap so ky tu can sao chep tinh tu vi tri sao chep sl ="; cin>>sl;

cout<<" chuoi moi

Chuoi s nguon:"<<s;

strcpy(s+vt-1,s+vt+n-1) ;

cout<<"

Chuoi sau khi xoa:"<<s;

}

void main(){

clrscr();

char s[100];

cout<<"Nhap chuoi:"; cin.get(s,100);

int vt,n;

cout<<"

Nhap vi tri xoa:"; cin>>vt;

cout<<"

Nhap so ky tu can xoa:"; cin>>n;

xoa(s,vt,n);

getch();

}

//tìm chuỗi b trong chuỗi a, hiện ví trí xuất hiện của chuỗi b trong a

void main()

{

clrscr();

 char chuoilon[100], chuoinho[25];

cout<<"

nhap chuoi lon:";

gets(chuoilon);

cout<<"

nhap chuoi can tim:";

gets(chuoinho);

if(strstr(chuoilon,chuoinho)!='\0')

{

cout<<"

ky tu vua nhap xuat hien trong chuoi nguon";

int n=strlen(chuoilon);

int m=strlen(chuoinho);

int d=0;

for(int i=0;i<=n;i++)

{

if(chuoinho[d]==chuoilon[i])

{d++;

if(d==m)

cout<<"

S2 xuat hientai vi tri thu\t"<<(i+1-d+1)<<"\tden vi tri\t"<<i+1;

}

Else

d=0;}}

else

cout<<"

S2 khong xuat hien trong S1:";

getch();

}

Bạn đang đọc truyện trên: Truyen2U.Pro