原帖由 meiyuhan 于 2007-4-26 17:39 发表
晕,盯着看了半天,看懂了
原帖由 MMMIX 于 2007-4-26 18:16 发表
这不挺好嘛,非要到论坛上发贴问。
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."![]()
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;
- }
欢迎光临 Chinaunix (http://bbs.chinaunix.net/) | Powered by Discuz! X3.2 |