这一小节我就简单关注一下 字符数组的一个小细节。

比如, 当我们有一个字符串, “hello\n”, 显示在一个C程序当中。 事实上, 这是以字符数组“hello\n\0”的形式存储的。 (\0是空(null)字符, 其值为0)(\0在这里用来作标记结尾之用)

书后练习题1-17:

Write a program to print all input lines that are longer than 80 characters.

我写的版本:

#include <stdio.h>

#define MAX 1000

int getline_alt(char[], int);

int main() {  
        int counter;
        char line[MAX];

        counter = 0;
        while ((counter = getline_alt(line, MAX)) > 0)
                if (counter > 80)
                        printf("%s", line);

        printf("\n");
        return 0;
}

/*
Return:  
        positive: get line successfully. The values equals the length of the characters of line
        non-positive: there's nothing in the line
*/
int getline_alt(char line[], int limit) {  
        int result;
        result = 0;

        int c;
        while ((c=getchar())!=EOF && c!='\n' && (result<MAX-2)) {
                line[result++] = c;
        }

        if ((c == '\n') && (result < MAX-1)) {
                line[result++] = c;
        }

        if (result < MAX)
                line[result] = '\0';

        return result;
}

书后练习题1-18:

Write a program to remove trailing blanks and tabs from each line of input, and to delete entirely blank lines.

我写的版本:

#include <stdio.h>

#define MAX 1000

int getline_alt(char[], int);

int main() {  
    int counter;
    char line[MAX];

    counter = 0;

    while ((counter=getline_alt(line, MAX)) > 0) {
        printf("%s", line);
    }

    printf("\n");

    return 0;
}

/*
Return:  
    positive: get the line successfully and the returnvalue equals the length of the line that just read(after removing trailing blanks and tabs)
    non-positive: can not get the valid length of the line. 
*/
int getline_alt(char line[], int limit) {  
    int result;
    int c;

    result = 0;
    while (((c=getchar())!=EOF)&&(c!='\n')&&(result<limit-2)) {
        if ((c!='\t') && (c!=' ')) {
            line[result++] = c;
        }
    }

    if ((result<limit-1) && (c=='\n')) {
        if (result != 0) line[result++] = '\n';
    }

    line[result] = '\0';

    return result;
}

书后练习题1-19:

Write a function reverse(s) that reverses the character string s. Use it to write a program that reverses its input a line at a time.

我写的版本:

#include <stdio.h>

#define MAX 1000

void reverse(char[]);  
int getline_alt(char[], int); 

int main() {  
    int counter; 
    char line[MAX]; 

    while ((counter=getline_alt(line, MAX))>0) {
               reverse(line); 
        printf("%s", line); 
    }

    printf("\n"); 

    return 0; 
}

void reverse(char line[]) {  
    int count; 
    int i = 0; 
    while (line[i++]); 
    char backup_line[i]; 

    for (count=0; count<i-1; count++) {
        backup_line[count] = line[count]; 
    } 
    backup_line[i-1] = '\0';

    int ctl_num; 
    if (backup_line[i-2] == '\n') {
        ctl_num = 2; 
    } else {
        ctl_num = 1; 
    }
    for (count=0; count<i-ctl_num; count++) {
        line[(i-1-ctl_num)-count] = backup_line[count]; 
    }
    line[i-1] = '\0'; 
}

int getline_alt(char line[], int limit) {  
    int result; 
    int c; 

    result = 0; 

    while (((c=getchar())!=EOF) && (c!='\n') && (result<limit-2)) {
        line[result++] = c; 
    }

    if ((result<limit-1) && (c=='\n')) {
        line[result++] = c; 
    }

    line[result] = '\0'; 

    return result; 
}

参考: 《The C Programming Language》- Chapter 1.9