整理期中考试的第一题,有关字符串输入输出
题目描述:
输入一串字符,输出这串字符。hhhhh
看到的标准答案版本是
1.读取单个字符scanf/getchar+EOF
2.读取字符串fgets/gets_s/gets(只能读到换行符)
1 2 3 4 5 6 7 8 9 10 11
| 方法一 #include<stdio.h> int main() { char x; while(scanf("%c",&x)!=EOF) 条件也可以写成 ~scanf("%c",&x) \ scanf("%c",&x)==1 { printf("%c",x); } return 0; }
|
1 2 3 4 5 6 7 8 9 10 11
| 方法二 #include <stdio.h> int main(void) { char x; while((x=getchar())!=EOF) { printf("%c",x); } return 0; }
|
1 2 3 4 5 6 7 8 9
| 方法三 #include <stdio.h> int main(void) { char a[1026] = {0}; fgets(a, 1026, stdin); 只能读一行 printf("%s", a); return 0; }
|