- 论坛徽章:
- 0
|
Chris writes: "Looks like Mr. Kernighan meant for "main routine" in Exercise 1-16 to refer to function main(), saying your solution of modifying getline() is "too easy."
comp.lang.c应该是这方面最大的新闻组资源了吧
i think "Revising the main routine" means, just modifying the "main" function and that is what i did. i did not change anything in "getline" or "copy" functions.
solution by arnuld(mailto:geek.arnuld@gmail.com)
- /* K&R2: 1.9, Character Arrays, exercise 1.16
- STATEMENT:
- revise the main routine of the longest-line programme
- so it will correctly print the length of the arbitrarily
- long input lines including as much of th possible text.
- */
- #include<stdio.h>
- #define MAXLINE 1000
- int getline(char [], int max);
- void copy(char from[], char to[]);
- int main()
- {
- int len = 0; /* current line length */
- char line[MAXLINE]; /* current input line */
- while((len = getline(line, MAXLINE)) > 0)
- {
- printf("LENGTH: %d\n", len);
- printf("LINE-CONTENTS: %s\n", line);
- }
- return 0;
- }
- int getline(char line[], int max)
- {
- int i = 0; /* simply an index counter */
- int c = 0; /* variale to store user input */
- for(i = 0; ((c = getchar()) != EOF) && c != '\n' && i < max - 1; ++i)
- line[i] = c;
- if(c == '\n')
- line[i++] = c;
- line[i] = '\0';
- return i;
- }
复制代码
[ 本帖最后由 imonyse 于 2007-4-30 12:54 编辑 ] |
|