1
int demkhoangtrang(char *s)
{
int d=0;
while(strstr(s," ")!=NULL)
{
d++;
s=strstr(s," ")+1;
}
return d;
}
//lam theo ham de quy
// s=strstr(s," ");
//if(s==NULL)
//return 0;
//else return 1+demkhoangtrang(s+1);
2
void xoakhoangtrang(char *s)
{
char *c=strstr(s," ");
while(c!=NULL)
{
int t=strlen(s)-strlen©;
for(int i=t;i<strlen(s);i++)
s[i]=s[i+1];
c=strstr(s," ");
}
if(s[0]==' ')
s=s+1; //xoa khang trang dau chuoi
int n=strlen(s);
if(s[n-1]==' ')
s[n-1]='\0';//xoa khoang trang cuoi chuoi
cout<<s; //for(int i=0;i
//cout<
}
3
void noichuoi(char *a,char *b)
{
strcat(a,b);
puts(a);
}
4
void chuthuong(char *a)
{
for(int i=0;i<strlen(a);i++)
if(a[i]>=65 && a[i]<=90)
a[i]=a[i]+32;
puts(a);
}
5
void chuhoa(char *a)
{
for(int i=0;i<strlen(a);i++)
a[i]=toupper(a[i]); //if(a[i]>=97 && a[i]<=122)
puts(a); //a[i]=a[i]-32;
}
6
void chuhoadau(char *s)
{
s[0]=toupper(s[0]);
while(strstr(s," ")!=NULL)
{
s=strstr(s," ")+1;
s[0]=toupper(s[0]);
}
}
7
void chuxenke(char *a)
{
int n=strlen(a);
for(int i=0;i<n;i++)
{
if(i%2==0)
if((int)a[i]>=97 && (int)a[i]<=122)
a[i]=char((int)a[i]-32);
if(i%2==1)
if((int)a[i]>=65 && (int)a[i]<=90)
a[i]=char((int)a[i]+32);
}
puts(a);
}
8
void daochuoi(char *s)
{
puts(strrev(s));
}
9
int vitri(char *a,char *b)
{
int kq,d=0;
if(strstr(a,b)!=NULL)
{
kq=strlen(a)-strlen(strstr(a,b));
for(int i=kq;i>=0;i--)
if(a[i]==' ')
d++;
return d+1;
}
else return -1;
}
10
int diemtu(char *a,char *b)
{
int d=0;
while(strstr(a,b)!=NULL)
{
d++;
a=strstr(a,b)+1;
}
return d;
}
11
int vitri(char *a,char *b)
{
int d=-1;
if(strstr(a,b)!=NULL)
d=strlen(a)-strlen(strstr(a,b));
return d;
}
12
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\n";
}
13
void daochuoi(char *s)
{
char t[50],r[50];
int i,l;
for(i=strlen(s)-1;i>=0;i--)
if(s[i]==' ')
break;
strcpy(r,s+i+1);//tim dctu cuoi
s[i+1]='\0';//xau s sau khi bo tu cuoi,co khoang trang o cuoi xau
l=strlen(s)-strlen(strstr(s," ")); //tim vi tri khoang trang dau tien trong chuoi
strcpy(t,s+l+1);//l la xau chau cac tu chinh giua
s[l]='\0'; //hien tai la xau chua tu dautien
strcat(strcat(strcat(r," "),t),s);//noi cac xau lai voi nhau*/
puts®;
}
14
void cathoten(char *s)
{
char *t;
int i;
for(i=strlen(s)-1;i>=0;i--)
if(s[i]==' '&&s[i+1]!=' ')
break;
t=s+i+1;
s[i]='\0';
cout<<"ho lot: ";
puts(s);
cout<<"ho ten: ";
puts(t);
}
15
void timtu(char *a,char *b)
{
char *t=strstr(a,b);//vi tri xuat hien b dau tien trong chuoi a
puts(t);
}
16
bool ktdoixung(char *s)
{
char c[255];
strcpy(c,s);
if(strcmpi(s,strrev©)==0)
return true;
else return false;
}
17
void tachso(char *s)
{
int a[100],j=0;
for(int i=0;i<strlen(s);i++)
if(s[i]>='0' && s[i]<='9')
{ a[j]=s[i];//ki tu kieu char chuyen thanh kieu int(vd:1=>49)
j++;
for(int t=i;t<strlen(s);t++)
s[t]=s[t+1];
i--;
}
puts(s);
for(int i=0;i<j;i++)
cout<<char(a[i])<<" ";
}
18
void xoakitu(char *a,char b)
{
int n=strlen(a);
for(int i=0;i<n;i++)
if(a[i]==b)
{
for(int j=i;j<n;j++)
a[j]=a[j+1];
i--;
n--;
}
cout<<a;
}
19
void chuhoa(char *s)
{
s[0]=toupper(s[0]);
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;
}
puts(s);
}
20
void daothutu(char *s)
{
char c[255];
c[0]='\0';//ham strcat noi chuoi tai vi tri NULL
for(int i=strlen(s)-1;i>=0;i--)
if(s[i]==' ')
{
strcat(strcat(c,s+i+1)," ");
s[i]='\0';
}
strcat(c,s);
puts©;
}
21
void xoa(char *s,int vt,int n)
{
strcpy(s+vt,s+vt+n);
puts(s);
}
22
void chen(char *s,char *d,int vt)
{
char c[255];
strcpy(c,s+vt);
strcpy(s+vt,d);
strcat(s,c);
puts(s);
}
23
void xoatu(char *s,char *c)
{
while(strstr(s,c)!=NULL)
{
int t=strlen(s)-strlen(strstr(s,c)),d=strlen©;
if((s[t-1]==' '&& s[t+d]==' ')||(s[t-1]==' '&& t+d==strlen(s)))//truong hop tu giua va tu cuoi
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);
}
puts(s);
}
24
void kituxuathiennhieunhat(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(int i=1;i<(int)strlen(s);i++)
if(a[i]>max)
max=a[i];
for(int i=1;i<(int)strlen(s);i++)
if(a[i]==max)
cout<<"ki tu "<<s[i]<<" xuat hien nhieu nhat la "<<a[i]<<" lan\n";
}
25
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(int i=0;i<(int)strlen(s);i++)
cout<<"ki tu "<<s[i]<<" xuat hien "<<a[i]<<" lan\n";
0 nhận xét:
Đăng nhận xét