C 习题
2019-03-26 本文已影响0人
吃柠檬的鸮
练习 1 - 17 编写一个程序,打印长度大于 80 个字符的所有输入行。
/* ex17.c */
#include <stdio.h>
#define MAX 1000
#define PRINT_LIMIT 80
int mygetline(char line[], int limit) {
int len = 0;
int c;
if ((c = getchar()) == EOF) {
return 0;
}
while (c != '\n') {
line[len] = c;
++len;
if (len == (limit - 1))
break;
c = getchar();
}
line[len] = '\0';
return (len + 1);
}
void display(char line[]) {
int i = 0;
putchar('\n');
while (line[i] != '\0') {
putchar(line[i]);
++i;
}
putchar('\n');
}
int main() {
int max = 0;
int len = 0;
char line[MAX];
while ((len = mygetline(line, MAX)) > 0) {
if (len > PRINT_LIMIT) {
display(line);
}
}
return 0;
}
编译运行结果:
$ ./ex17.out
I went to the woods because I wished to live deliberately, to front only the essential facts of life, and see if I could not learn what it had to teach, and not, when I came to die, discover that I had not lived.
I went to the woods because I wished to live deliberately, to front only the essential facts of life, and see if I could not learn what it had to teach, and not, when I came to die, discover that I had not lived.
I did not wish to live what was life, living is so dear; nor did I wish to practise resignation, unless it was quite necessary.
I did not wish to live what was life, living is so dear; nor did I wish to practise resignation, unless it was quite necessary.
I did not wish to live what was life, living is so dear;
$
练习 1 - 18 编写一个程序,删除每个行末尾的空格及制表符,并删除完全是空格的行。
解题思路:这个程序基本上是在上一题程序的基础上修改的,在读取完一整行后,先调用 remove 函数,从当前行的最后一个字符往前判断当前字符是否为空白字符。
int removeSpace(char line[], int len) {
while (line[len - 1] == ' ' || line[len - 1] == '\t') {
--len;
}
return len;
}
完整的代码清单如下:
/* ex18.c */
#include <stdio.h>
#define MAX 1000
int mygetline(char line[], int limit) {
int len = 0;
int c;
if ((c = getchar()) == EOF) {
return 0;
}
while (c != '\n') {
line[len] = c;
++len;
if (len == (limit - 1))
break;
c = getchar();
}
len = removeSpace(line, len);
line[len] = '\0';
return (len + 1);
}
int removeSpace(char line[], int len) {
while (line[len - 1] == ' ' || line[len - 1] == '\t') {
--len;
}
return len;
}
void display(char line[]) {
int i = 0;
while (line[i] != '\0') {
putchar(line[i]);
++i;
}
putchar('\n');
putchar('\n');
}
int main() {
int max = 0;
int len = 0;
char line[MAX];
while ((len = mygetline(line, MAX)) > 0) {
if (line[0] != '\0')
display(line);
}
return 0;
}
练习 1 - 19 编写函数 reverse(s),将字符串 s 中的字符顺序颠倒过来。使用该函数编写一个程序,每次颠倒一个输入行中的字符顺序。
reverse(s) 函数的设计思路:由于没有传入数组的长度,因此需要先找到最后一个字符的索引值,然后定义一个中间变量用于交换首尾两端的字符。
void reverse(char line[]) {
/* find the end of the line */
int end;
int i = 0;
int tmp = 0;
while (line[i++] != '\0') {
;
}
end = i - 2;
i = 0;
while (i < (end - i)) {
tmp = line[i];
line[i] = line[end - i];
line[end - i] = tmp;
++i;
}
}
完整代码清单如下:
/* ex19.c */
#include <stdio.h>
#define MAX 1000
void reverse(char line[]) {
/* find the end of the line */
int end;
int i = 0;
int tmp = 0;
while (line[i++] != '\0') {
;
}
end = i - 2;
i = 0;
while (i < (end - i)) {
tmp = line[i];
line[i] = line[end - i];
line[end - i] = tmp;
++i;
}
}
int mygetline(char line[], int limit) {
int len = 0;
int c;
if ((c = getchar()) == EOF) {
return 0;
}
while (c != '\n') {
line[len] = c;
++len;
if (len == (limit - 1))
break;
c = getchar();
}
line[len] = '\0';
reverse(line);
return (len + 1);
}
void display(char line[]) {
int i = 0;
while (line[i] != '\0') {
putchar(line[i]);
++i;
}
putchar('\n');
putchar('\n');
}
int main() {
int max = 0;
int len = 0;
char line[MAX];
while ((len = mygetline(line, MAX)) > 0) {
if (line[0] != '\0')
display(line);
}
return 0;
}
编译运行结果:
$ ./ex19.out
1234567890 abcdefghijklmnopqrstuvwxyz
zyxwvutsrqponmlkjihgfedcba 0987654321