该书英文配套答案
Answer to Exercise -, page
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);
void copy(char to[], char from[]); /* 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;
}
}//这里getline修改后,i可以大于lim限制,只计数,不保存字符。
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;
}
}

练习1-16:修改打印最长文本行的程序的主程序main,使之可以打印任意长度的输入行的长度,并尽可能多地打印文本(C程序设计语言 第2版)的更多相关文章

  1. C程序设计语言(第二版)习题:第一章

    第一章虽然感觉不像是个习题.但是我还是认真去做,去想,仅此而已! 练习 1-1 Run the "hello, world" program on your system. Exp ...

  2. C语言学习书籍推荐《C程序设计语言(第2版•新版)》下载

    克尼汉 (作者), 等 (作者, 译者), 徐宝文 (译者) 下载地址:点我 <C程序设计语言(第2版•新版)>是由C语言的设计者Brian W.Kernighan和Dennis M.Ri ...

  3. c++学习书籍推荐《C++程序设计语言(特别版)》下载

    百度云及其他网盘下载地址:点我 编辑推荐 <C++程序设计语言(特别版•十周年中文纪念版)>编辑推荐:十周年纪念版,体味C++语言的精妙与魅力,享受与大师的心灵对话.1979年,Biarn ...

  4. 练习1-13:编写一个程序,打印输入中单词长度的直方图(水平)(C程序设计语言 第2版)

    简单未考虑标点符号 #include <stdio.h> #define MAX_WORD_LEN 10 #define BLANK 0 #define IN_WORD 1 #define ...

  5. 练习1-12:编写一个程序,以每行一个单词的形式打印其输入(C程序设计语言 第2版)

    #include <stdio.h> #define NOT_BLANK 1 #define BLANK 0 main() { int c; int last_ch = NOT_BLANK ...

  6. 练习2-3:十六进制数字字符串转换为等价整型值,字符串允许包含的数字包括:0~9、a~f、A~F、x、X(C程序设计语言 第2版)

    #include <stdio.h> #include <string.h> #include <math.h> int htoi(char s[]){ unsig ...

  7. 练习1-21:编写程序entab,将空格串替换为最少数量的制表符和空格。。。(C程序设计语言 第2版)

    #include <stdio.h> #define N 5 main() { int i, j, c, lastc; lastc = 'a'; i = j = ; while ((c=g ...

  8. 练习1-23:删去C语言程序中所有的注释语句(C程序设计语言 第2版)

    #include <stdio.h> main() { FILE * fp_i; FILE * fp_o; fp_i = fopen("input.txt", &quo ...

  9. C程序设计语言(第二版)习题:第二章

    这一章习题做着很舒服,毕竟很简单.所以很有感觉. 练习 2-1 Write a program to determine the ranges of char , short , int , and ...

随机推荐

  1. 被忽略的js细节

    今天在写一个程序,出了一点小bug,找了许久许久,终于把问题给揪了出来,真相大白那一刻感觉好蛋疼--深刻体会到了语言之间的差异. <script type="text/javascri ...

  2. web页面全角&半角

    根据Unicode编码,全角空格为12288,半角空格为32 : 其他字符半角(33-126)与全角(65281-65374)的对应关系是:均相差65248  全角-->半角函数  //半角转换 ...

  3. AppScan 测试需要输入用户名密码的网站

    Cisco有专门的网页版的AppScan,使用前需要向有关的team申请account和权限. account和权限申请成功后,登录AppScan网站,创建自己的文件夹目录,然后在自己的目录下新建sc ...

  4. LVS_DR模式构建配置

    一.环境准备 lvs负载均衡器 系统:centos6.8 ip:192.168.2.203 vip:192.168.2.17 web服务器RS1 系统:centos6.8 ip:192.168.2.2 ...

  5. js进阶

    js在实例化对象后,可以对这个对象增加属性和属性值,并且还可以通过delete一元操作符来删除对象的属性. var o = new Object(); o.name = "langsin&q ...

  6. nodejs npm常用命令

    npm是一个node包管理和分发工具,已经成为了非官方的发布node模块(包)的标准.有了npm,可以很快的找到特定服务要使用的包,进行下载.安装以及管理已经安装的包. 1.npm install m ...

  7. gulp插件autoprefixer

    gulp的autoprefixer插件可以根据我们的设置帮助我们自动补全浏览器的前缀(如:-moz.-ms.-webkit.-o) 1)首先安装gulp,不知道怎么安装请看这里 2)安装autopre ...

  8. 四元数(Quaternions)简介

    经常在代码中看到Quaternions,也知道它是用来表达三维空间的旋转的,但一直没有更深的理解.这两天终于花点时间看了看维基百科的介绍,算是多了解了点.做个记录吧! 本质上而言,四元数是一个数学概念 ...

  9. HTML5.dcloud.io-stream-app

    dcloud.io提出的Stream App 本文仅仅是关于dcloud.io提出的SteamApp初探,所有内容请参考其官网. 1. Application promotion by scaning ...

  10. 漫谈Linux内核哈希表(1)

    关于哈希表,在内核里设计两个很重要的数据结构:    哈希链表节点: 点击(此处)折叠或打开 .x [include/linux/types.h]*/ struct hlist_node { stru ...