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. InstallShield : 如何查找编译后的 Merge Module存放路径

    工程菜单栏中依次选择  Tools ---> Options… ,选择 Merge Modules tab 页,如下,就会看到Merge Module的存放路径,也可以根据需求修改. Merge ...

  2. java.lang.StringBuilder源码分析

    StringBuilder是一个可变序列的字符数组对象,它继承自AbstractStringBuilder抽象类.它不保证同步,设计出来的目的是当这个字符串缓存只有单线程使用的时候,取代StringB ...

  3. 建立IP6隧道

    某站点又开始全站Free了,是否还在为在家上不了IPv6站点而苦恼呢?本教程适用于路由后的windows设备,即ip地址为内网地址通过本教程设置,可实现windows设备获得ipv6地址,以访问IPv ...

  4. mysql show processlist 显示mysql查询进程

    1.进入mysql/bin目录下输入mysqladmin processlist; 2.启动mysql,输入show processlist; 如果有 SUPER 权限,则可以看到全部的线程,否则,只 ...

  5. leetcode Largest Rectangle in Histogram 单调栈

    作者:jostree 转载请注明出处 http://www.cnblogs.com/jostree/p/4052343.html 题目链接 leetcode Largest Rectangle in ...

  6. winform 通过 html 与swf 交互 简单案例

    在上一篇 winform 与 html 交互 简单案例 中讲了winform与html之间的简单交互,接下来的内容是在winform中以html为中转站,实现将swf嵌入winform中并实现交互. ...

  7. Unity3D--学习太空射击游戏制作(一)

    近期买了本书在学习一些Unity3D的东西,在了解了Unity3D工具的基本面板后开始学习一个太空射击游戏的开发过程. 首先下载一个关于本游戏的资源文件,(百度云下载地址:http://pan.bai ...

  8. php class类的用法详细总结

    以下是对php中class类的用法进行了详细的总结介绍,需要的朋友可以过来参考下 一:结构和调用(实例化): class className{} ,调用:$obj = new className(); ...

  9. SQL语句中使用条件逻辑

    select name, sal, case when sal >= 4000 then 'Good' when sal <= 2000 then 'Bad' else 'Ok' end ...

  10. python 时间及日期函数

    本人最近新学python ,用到关于时间和日期的函数,经过一番研究,从网上查找资料,经过测试,总结了一下相关的方法. import timeimport datetime '''时间转化为时间戳: 2 ...