c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
2014-04-06 09:51
435 查看
Revise the main routine of the longest-line program so it will correctly print the length of arbitrarily long input lines, and as much as possible of the text.
Chris Sidi, however, was not convinced - he thought
this answer was "too easy", so he checked with bwk, who agreed. 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." :)
(Though I think your solution shouldn't be removed from the Answers web site,
just complimented with another one that only modifies main())"
Cue Mr
"386sx", riding to the rescue on a white horse...
/* This is the first program exercise where the spec isn't entirely * clear. The spec says, 'Revise the main routine', but the true * length of an input line can only be determined by modifying * getline. So that's what we'll do. getline will now return the * actual length of the line rather than the number of characters * read into the array passed to it. */ #include <stdio.h> #define MAXLINE 1000 /* maximum input line size */ int getline(char line[], int maxline); //自己编写getline()函数,接收整行字符串 void copy(char to[], char from[]); //和c语言库函数strcpy()实现同样的功能。 /* print longest input line */ int main(void) { int len; /* current line length */ int max; /* maximum length seen so far */ char line[MAXLINE]; /* current input line */ char longest[MAXLINE]; /* longest line saved here */ max = 0; while((len = getline(line, MAXLINE)) > 0) { printf("%d: %s", len, line); if(len > max) { max = len; copy(longest, line); } } if(max > 0) { printf("Longest is %d characters:\n%s", max, longest); } printf("\n"); return 0; } /* getline: read a line into s, return length */ int getline(char s[], int lim) { int c, i, j; for(i = 0, j = 0; (c = getchar())!=EOF && c != '\n'; ++i) { if(i < lim - 1) { s[j++] = c; } } if(c == '\n') { if(i <= lim - 1) { s[j++] = c; } ++i; } s[j] = '\0'; return i; } /* copy: copy 'from' into 'to'; assume 'to' is big enough */ void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '\0') { ++i; } }
Chris Sidi, however, was not convinced - he thought
this answer was "too easy", so he checked with bwk, who agreed. 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." :)
(Though I think your solution shouldn't be removed from the Answers web site,
just complimented with another one that only modifies main())"
Cue Mr
"386sx", riding to the rescue on a white horse...
/* Exercise 1-16 */ #include <stdio.h> #define MAXLINE 20 int getline(char s[], int lim); void copy(char to[], char from[]); int main(void) { char line[MAXLINE]; char longest[MAXLINE]; char temp[MAXLINE]; int len, max, prevmax, getmore; max = prevmax = getmore = 0; while((len = getline(line, MAXLINE)) > 0) { //蛋疼啊,不写注释,看不懂。 if(line[len - 1] != '\n') { if(getmore == 0) copy(temp, line); prevmax += len; if(max < prevmax) max = prevmax; getmore = 1; } else { if(getmore == 1) { if(max < prevmax + len) { max = prevmax + len; copy(longest, temp); longest[MAXLINE - 2] = '\n'; } getmore = 0; } else if(max < len) { max = len; copy(longest, line); } prevmax = 0; } } if(max > 0) { printf("%s", longest); printf("len = %d\n", max); } return 0; } //重新实现getline,使得getline的容错性更强。接收后的getline一定以'\n'结束。 int getline(char s[], int lim) { int c, i; for(i = 0; i < lim - 1 && ((c = getchar()) != EOF && c != '\n'); ++i) s[i] = c; if(c == '\n') { s[i] = c; ++i; } else if(c == EOF && i > 0) { /* gotta do something about no newline preceding EOF */ s[i] = '\n'; ++i; } s[i] = '\0'; return i; } void copy(char to[], char from[]) { int i; i = 0; while((to[i] = from[i]) != '\0') ++i; }
相关文章推荐
- C语言程序设计习题1-19 编写函数reverse(s),实现字符串的倒序,并用该函数每次颠倒一个输入行中的字符顺序
- c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。
- C语言程序设计习题 1-9 编写一个将输入复制到输出的程序,并将其中连续多个空格用一个空格代替
- 习题 7.9 编写一个函数,由实参传来一个字符串,统计此字符串中字母、数字、空格、和其他字符的个数,在主函数中输入字符串以及输出上述的结果。
- sql语句 自己编写一个函数reversion,完成颠倒一个字符串,即:select dbo.reversion('abcd') 输出的结果为 dcba
- 编写一个函数,实现两个字符串的比较,即自己写一个strcmp函数(考察指针传参)
- 请编写一个C函数,该函数可以实现将一个整数转为任意进制的字符串输出
- 编程:编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如"我ABC"4,应该截为"我AB",输入"我ABC汉DEF",6,应该输出为"我ABC"而不是"我ABC+汉的半个"。
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 但是要保证汉字不被截半个,如“我ABC”4,应该截为“我AB”,输入“我ABC汉DEF”,6,应该输出为“我ABC”而不是“我ABC+汉的半个”。
- 请编写函数fun(char *s),其功能是:删除字符串中的数字字符。主函数中输入字符串,调用函数,并输出结果字符串。例如若输入的字符串是:34AB9C6DE, 则应输出:ABCDE
- 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串,但要保证汉字不被截取半个,如“我ABC”,4,应该截取“我AB”,输入“我ABC汉DEF”,6,应该输出“我ABC”,而不
- 10、 java 编写一个截取字符串的函数,输入为一个字符串和字节数,输出为按字节截取的字符串。 要求不能出现截半的情况
- 练习 3-5 编写函数 itob(n, s, b),将整数n转换为以b为底的数,并将转换结果以字符的形式保存到字符串s中。例如,itob(n, s, 16)把整数n格式化成十六进制整数保存在s中。
- 编写一函数,由实参传来一个字符串,统计此字符串中字母、数字、空格和其它字符的个数,在主函数中输入字符串以及输出上述结果。 只要结果,别输出什么提示信息。
- 习题 8.17 写一函数,实现两个字符串的比较。即自己写一个strcmp函数,函数原型为int strcmp(char *p1, char *p2);
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
- //1、有一个字符串开头或结尾含有n个空格(” abcdefgdddd ”),欲去掉前后空格,返回一个新字符串。 //要求1:请自己定义一个接口(函数),并实现功能;70分 //要求2:编写
- (3) 编写一个截取字符串的函数,输入一个字符串和字节数,输出按字节书截取的字符串,但是要保证汉字不能截半个
- 自己编写的去掉字符串中的所有空格函数trim_all