#include <stdio.h>
#include <conio.h>
#include <stdlib.h>
#include <string.h>
#include <iostream.h>
void nhapchuoi(char *a)
{
printf("Nhap chuoi ki tu: ");
gets(a);
}
void xuatchuoi(char *a)
{
printf("
CHuoi la: %s",a);
}
...............................
void daochuoi(char *a)
{
int n = strlen(a);
printf("
n = %d
",n);
int i = 0;
char c;
for ( i = 0;i < n/2 ; i++)
{
//printf("%c", a[i]);
c = a[i];
a[i] = a[n -i-1];
a[n -i-1] = c;
//printf("%c", a[i]);
//printf("%c",a[n-i +1]);
}
}
.................................
void xoa_kt(char *a)
{
int n = strlen(a);
int i =0;
int j;
do
{
if(a[i] == ' ')
{
for(j = i; j<n ; j++)
{
a[j] = a[j+1];
}
//free(a);
i = i -1;
n = n-1;
}
i = i +1;
}
while(i<n);
xuatchuoi(a);
}
................................
Loại bỏ khoảng trống thừa trong chuỗi
#include <stdio.h>
#include <string.h>
#include <conio.h>
#pragma warn -pia
char *trim(char *chuoi)
{
char *p;
while (p = strstr(chuoi, " "))
memmove(p, p+1, strlen(chuoi) - (p - chuoi));
if (chuoi[0] == ' ')
memmove(chuoi, chuoi+1, strlen(chuoi) - 1);
if (chuoi[strlen(chuoi)-1] == ' ')
chuoi[strlen(chuoi)-1] = 0;
return chuoi;
}
void main()
{
char chuoi[125];
printf("
Nhap chuoi mau : ");
textattr(0x1e);
gets(chuoi);
trim(chuoi);
printf("
Chuoi sau khi da trim : ");
textattr(0x1e);
cputs(chuoi);
getch();
}
........................................
Đếm ký tự trong 1 tập tin
#include <stdio.h>
#include <ctype.h>
void main()
{
FILE *fp;
char sf[50], c;
long count = 0;
printf("
Nhap vao ten tap tin : ");
scanf("%s", sf);
if ((fp = fopen(sf, "r")) == NULL)
fprintf(stderr, "
Khong the mo tap tin %s", sf);
else
{
while ((c = fgetc(fp)) != EOF)
if (isalpha©)
count++;
printf("
So ky tu trong %s la %ld", sf, count);
}
getch();
}