C 习题

作者: 吃柠檬的鸮 | 来源:发表于2019-03-26 19:51 被阅读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

相关文章

  • C 习题

    练习 1 - 12 编写一个程序,以每行一个单词的形式打印其输出。 编译运行结果:

  • C 习题

    练习 1 -13 编写一个程序,打印输入中单词长度的直方图。 编译运行结果如下:

  • C 习题

    练习 1 - 17 编写一个程序,打印长度大于 80 个字符的所有输入行。 编译运行结果: 练习 1 - 18 编...

  • C语言练习题: 函数部分

    C语言练习题:函数部分(9题) 上一篇: C语言练习题:循环部分 下一篇: C语言练习题:数组部分 斐波那契,函数...

  • C语言练习题:循环部分

    C语言练习题:循环部分(20题) 上一篇: C语言练习题:if语句部分 下一篇: C语言练习题:函数部分 求一正整...

  • 第2章练习题答案

    习题1 答: 习题2 答: 习题3 答: 习题4 如果使用gcc -wall -o e4 e4.c编译,则有警告信...

  • C++ Primer Plus习题及答案-第九章

    C++ Primer Plus习题及答案-第九章 习题选自:C++ Primer Plus(第六版)内容仅供参考,...

  • 软考-项目管理(下)

    习题答案 1.1 - 1.9 C A D C D B D A D2.1 - 2.6 C D D D A C 知识点...

  • 软考-法律法规(下)

    习题答案 1.1 - 1.9 A A A B C C B D C2.1 - 2.4 D D D B3.1 - 3....

  • 软考-测试调试(下)

    习题答案 1.1 - 1.6 A B A C D C2.1 - 2.8 B D C D B B A B 知识点整理...

网友评论

      本文标题:C 习题

      本文链接:https://www.haomeiwen.com/subject/bkogvqtx.html