
chuoi
18/ tổng hợp chuỗi
#include<iostream.h>
#include<conio.h>
#include<string.h>
#include<ctype.h>
// hàm chuẩn hóa tên.(cắt khoảng trắng)
void xoakhoangtrang(char *s)
{
char *c=strstr(s," ");
while(c!=NULL)
{
int t=strlen(s)-strlen(c);
for(int i=t;i<strlen(s);i++)
s[i]=s[i+1];
c=strstr(s," ");
}
if(s[0]==' ')
s=s+1;
int n=strlen(s);
if(s[n-1]==' ')
s[n-1]='\0';
cout<<s;
}
//viết hoa chữ cái đầu
void xuli(char *s)
{if((s[0]>=97)&&(s[0]<=122))
s[0]-=32;
int i=0;
while(s[i]!='\0')
{ if((s[i]==' ')&&(s[i+1]>=97)&&(s[i+1]<=122))
s[i+1]-=32;
i++;
}
cout<<s;
}
//hàm chuyển chuỗi thành chữ thường
void chuthuong(char *s)
{
for(int i=0;i<strlen(s);i++)
if(s[i]>=65 && s[i]<=90)
s[i]=s[i]+32;
cout<<s;
}
//hàm chuyển chuỗi thành chữ hoa
void chuhoa(char *s)
{
for(int i=0;i<strlen(s);i++)
s[i]=toupper(s[i]);
cout<<s;
}
//hàm xem kẽ chuỗi chữ thường chữ hoa
void chuxenke(char *s)
{
int n=strlen(s);
for(int i=0;i<n;i++)
{
if(i%2==0)
if((int)s[i]>=97 && (int)s[i]<=122)
s[i]=char((int)s[i]-32);
if(i%2==1)
if((int)s[i]>=65 && (int)s[i]<=90)
s[i]=char((int)s[i]+32);
}
cout<<s;
}
//hàm đảo chuỗi
void daochuoi(char *s)
{
for(int i=strlen(s);i>=0;i--)
cout<<s[i];
}
//hàm cắt họ+tên lót, tên
void cathoten(char *s)
{
char *t;
int i;
for(i=strlen(s);i>=0;i--)
if(s[i]==' '&&s[i+1]!=' ')
break;
t=s+i+1;
s[i]='\0';
cout<<endl<<"ho va ten lot:";
cout<<s<<endl;
cout<<endl<<"ten:";
cout<<t<<endl;
}
//hàm tách tên lót
void tachtenlot(char *s)
{
int j=0;
for(int i=strlen(s)-1;s[i]!=' ';i--)
j++;
for(i=strlen(s)-j;i<strlen(s);i++)
cout<<s[i];
}
//hàm tách họ
void tachho(char *s)
{
for(int i=0;s[i]!=' ';i++)
{cout<<s[i];}
}
//hàm tách tên
void tachten(char *s)
{
char *t;
int i;
for(i=strlen(s);i>=0;i--)
if(s[i]==' '&&s[i+1]!=' ')
break;
t=s+i+1;
s[i]='\0';
cout<<endl<<"tach ten:";
cout<<t<<endl;
}
void main()
{
clrscr();
char s[100];
cout<<"nhap chuoi:";cin.get(s,100);
cout<<endl<<"Chuan hoa chuoi:";
xoakhoangtrang(s);
cout<<endl<<"Chuoi chu thuong:";
chuthuong(s);
cout<<endl<<"viet hoa chu cai dau:";
xuli(s);
cout<<endl<<"Chuoi chu hoa:";
chuhoa(s);
cout<<endl<<"Chuoi Dao Nguoc:";
daochuoi(s);
cout<<endl<<"Xen ke chuoi:";
chuxenke(s);
cathoten(s);
cout<<"ten lot:";
tachtenlot(s);
cout<<endl<<"tach ho:";
tachho(s);
cout<<endl;
getch();
}
19/ lọc ra kí tự xuất hiện nhiều nhất trong chuỗi
#include<ctype.h>
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();
}
20/ xóa chuỗi với vị trí xóa và số kí tự cần xóa nhập phím
#include<string.h>
#include<ctype.h>
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();
}
21/ xóa một từ trong chuỗi với từ được nhập từ phím
#include<string.h>
#include<ctype.h>
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);
if(s[t+d]==' '&& t==0)
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();
}
23/ xóa 1 kí tự bất kì trong chuỗi với kí tự nhập từ phím
#include<string.h>
#include<ctype.h>
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();
}
24/ thống kê số lần xuất hiện của từng kí tự 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();}
26/ cắt ra chuỗi mới với vị trí bắt đầu cắt và số kí tự cắ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);
// 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 b=";
for(int j=vt-1; j<=vt+sl-2; j++)
cout<<a[j];
getch();
}
Bạn đang đọc truyện trên: Truyen247.Pro