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.

/* 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 = ; while((len = getline(line, MAXLINE)) > )
{
printf("%d: %s", len, line); if(len > max)
{
max = len;
copy(longest, line);
}
}
if(max > )
{
printf("Longest is %d characters:\n%s", max, longest);
}
printf("\n");
return ;
} /* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i, j; for(i = , j = ; (c = getchar())!=EOF && c != '\n'; ++i)
{
if(i < lim - )
{
s[j++] = c;
}
}
if(c == '\n')
{
if(i <= lim - )
{
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 = ;
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 = ;
while((len = getline(line, MAXLINE)) > )
{
     //蛋疼啊,不写注释,看不懂。
if(line[len - ] != '\n')
{
if(getmore == )
copy(temp, line);
prevmax += len;
if(max < prevmax)
max = prevmax;
getmore = ;
}
else
{
if(getmore == )
{
if(max < prevmax + len)
{
max = prevmax + len;
copy(longest, temp);
longest[MAXLINE - ] = '\n';
}
getmore = ;
}
else if(max < len)
{
max = len;
copy(longest, line);
}
prevmax = ;
}
}
if(max > )
{
printf("%s", longest);
printf("len = %d\n", max);
} return ;
}

//重新实现getline,使得getline的容错性更强。接收后的getline一定以'\n'结束。
int getline(char s[], int lim)
{
int c, i; for(i = ;
i < lim - && ((c = getchar()) != EOF && c != '\n');
++i)
s[i] = c; if(c == '\n')
{
s[i] = c;
++i;
}
else if(c == EOF && i > )
{
/* 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 = ;
while((to[i] = from[i]) != '\0')
++i;
}
												

c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出的更多相关文章

  1. c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号

    Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...

  2. c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)

      fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...

  3. c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()

    The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...

  4. c程序设计语言_习题1-19_编写函数reverse(s)将字符串s中字符顺序颠倒过来。

    Write a function reverse(s) that reverses the character string s . Use it to write a program that re ...

  5. c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行

    Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...

  6. c程序设计语言_习题1-13_统计输入中单词的长度,并且根据不同长度出现的次数绘制相应的直方图

    Write a program to print a histogram of the lengths of words in its input. It is easy to draw the hi ...

  7. c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件

    How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...

  8. c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格

    Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...

  9. 《c程序设计语言》读书笔记-4.2-扩充atof函数

    #include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...

随机推荐

  1. 【HeadFirst设计模式】13.与设计模式相处

    模式: 是在某情境下,针对某问题的某种解决方案. 要点: 让设计模式自然而然地出现在你的设计中,而不是为了使用而使用. 设计模式并非僵化的教条,你可以依据自己的需要采用或者进行调整. 总是使用最简单的 ...

  2. unix 常用命令

    (一)基本命令 命令格式: 命令 参数 1.ls 显示文件名,等同于dos下dir命令 命令格式:ls [option] file option: -l 显示详细列表 域1 :文件类型和文件权限 域2 ...

  3. 火狐flash插件

    1.解压缩文件: tar -xzvf ***.tar.gz 会解出一个文件:libflashplayer.so 和一个目录 usr 2.将文件libflashplayer.so 拷贝到目录   /us ...

  4. c#抽象工厂类

    抽象工厂类的结构如下: 工厂 a=new 1工厂 抽象类A aa=a.createA() aa.create()==类A1.create() 抽象类B bb=a.createB() bb.get()= ...

  5. WinForm 图形报表控件

    http://wenku.baidu.com/link?url=hOCeHErshNOw6NScDG3Y6JjT1mD-A4xHhjthcHyrTgk5NmPRKf0eqeaee4LmKZX5jd7S ...

  6. 关于css中overflow:hidden的使用

    overflow:hidden有两个用处经常用到: 1.通过设定自身的高度,加上overflow:hidden可以隐藏超过容器本身的内容:     但是,小编在以往的使用中,发现了一个问题,只要父级容 ...

  7. Python 多进程

    import threading from time import sleep from msalt_proxy.client import Client def f(t): print t cli= ...

  8. json在线校验

    弄了一个在线校验,清爽无广告,欢迎大家收藏   http://www.zhhoney.com/

  9. 说说对C语言指针的理解

    指针困扰了一些学习编程的人,或许你的老师会告诉你,指针比较难理解. 我当时被老师的话唬住所以学习指针那章的时候都没心情听课.(说得像讲别的内容时我听了似的,开玩笑) 导致了学习链表的时候各种卧槽. * ...

  10. UML_时序图画法

    UML建模之时序图(Sequence Diagram) 一.时序图简介(Brief introduction) 二.时序图元素(Sequence Diagram Elements) 角色(Actor) ...