Thứ Năm, 5 tháng 2, 2015

5.2

I.Lý Thuyết
Định nghĩa ...Là một chuỗi kí tự kết thúc bằng kí tự NULL
Định Nghĩa không thể bịa nên em chỉ chép đúng như những lời cô giảng :-ss
II-Bài Tập.
Bài 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"

#include <stdio.h>
#include <stdlib.h>

/* run this program using the console pauser or add your own getch, system("pause") or input loop */

int main(int argc, char *argv[]) {
    char a[6]={'c','s','c','2','6','1'};
    int i;
    for(i=5;i>=0;i--)
    printf("\ta[%d]=%c",i,a[i]);
    return 0;
}
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.

char a[30];
int i,j=0;
printf("Nhap Chuoi Ki Tu :");
scanf("%c",&a);

for (i=0;i<30;i++)
if (a[i] != 0)

j++;
printf("so ki tu trong chuoi la: %d",j-5);

return 0;
Bài 3 :
Write a program that counts the number of words in a string
 #include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
int i,j;
printf("Nhap Chuoi: ");
scanf("%c",&a);
j=1;
for(i=0;i<30;i++) if (a[i]==' ') j=j+1;
printf("So Tu: %d ",j);

return 0;
}
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.
#include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
char b[30];
printf("Nhap Chuoi: ");
scanf("%c",&a);
printf("Nhap Chuoi : ");
scanf("%c",&b);
if(a,b==0){

printf("a va b bang nhau");}

else {
printf("a va b khong bang nhau");

}

return 0;
}
Bài 5 :
Write a function that scans a character array for the character - and replaces it with _

 

 
#include <stdio.h>
#include <stdlib.h>

/* Write a program that counts the number of words in a string */

int main(int argc, char *argv[]) {
char a[30];
int i;
printf("Nhap Chuoi: ");
scanf("%s",&a);
for(i=0;i<30;i++){
if(a[i]=='-')
a[i]='_';
}
printf("%s",a);

return 0;
}



Không có nhận xét nào:

Đăng nhận xét