Bài 1:1. Write a program which prints the letters in a char array in reverse order. For
example, if the array contains {'c', 's', 'c', '2', '6', '1'}the output (to the terminal)
should be "162csc"
Code:char a[10];
int i;
for(i=0;i<9;i++){
printf("Xin moi nhap ki tu thu
%d: ",i);
scanf("%s",&a[i]);
}
for(i=8;i>=0;i=i-1){
printf("%c",a[i]);
}\
Bài 2: Write a program : declare a char array and the size of the array (of type char). This function counts the number of digit letters in the char array.
Code:char a[10];
int i,s=0;
for(i=0;i<10;i++){
printf("Xin moi nhap ki tu %d:
",i+1);
scanf("%s",&a[i]);
}
for(i=0;i<10;i++){
if(a[i]>47 && a[i]<58){
s=s+1;
}
}
printf("Trong
mang co %d phan tu la so",s);
Bài 3: Write a program that counts the number of words in a string
code:char
a[100];
int i,s=0;
printf("Nhap
Chuoi: ");
gets(&a);
for(i=0;i<100;i++){
if(a[i]==' '){
s=s+1;
}
}
Bài 4:Write a program that accept two string, after checking if string1 equals string2 then print 'string 1 equals string2', else print string1 less than or greater than string 2.
Code:char a[100],b[100];
int i,s=0,t=0;
printf("Nhap Chuoi: ");
gets(&a);
for(i=0;a[i]!='\0';i++){
if(a[i]==' '){
s=s+1;
}
}
printf("Nhap Chuoi: ");
gets(&b);
for(i=0;b[i]!='\0';i++){
if(b[i]==' '){
t=t+1;
}
}
if(t>s){
printf("Array 2 greater than array 1");
}
else if(s==t){
printf("2 array is equal");
}
else{
printf("Array 1 greater than array 2");
}
Bài 5:Write a function that scans a character array for the character - and replaces it with _
Code:char
a[100];
int i;
printf("Nhap
Chuoi: ");
gets(&a);
for(i=0;a[i]!='\0';i++){
if(a[i]=='-'){
a[i]='_';
}
}
printf("Day sau khi doi:
%s",a);
Nhận xét
Đăng nhận xét