该书英文配套答案
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. java_js从字符串中截取数字

    var str="1件*20桶*30包*123.45公斤"; var res=str.match(/\d+(\.\d+)?/g); alert(res);

  2. c#.net全站防止SQL注入类的代码

    using System;using System.Collections.Generic;using System.Linq;using System.Web; /// <summary> ...

  3. 给div设置background-color: rgba(0, 0, 0, 0.2)属性,并加了css3动画--opacity动画淡出动画,之后div子元素的字体会抖一下

    问题:给div设置background-color: rgba(0, 0, 0, 0.2)属性,并加了css3动画--opacity动画淡出动画,之后div子元素的字体会抖一下: 解决:animati ...

  4. 从tomcat启动到springIoC容器初始化(编辑中)

    tomcat的启动一般是从startup.bat/startup.sh开始,然后启动catalina.bat/catalina.sh,然后启动bootstrap.jar包 那么它们启动的时候都做了哪些 ...

  5. jquery mobile RedirectToAction url地址不更新

    使用asp.net mvc 和 jquery mobile 开发手机版网站 发起一个post请求,在第一个action里做了处理,用RedirectToAction 跳转到其他action继续处理后, ...

  6. 个人理解的javascript作用域链与闭包

    闭包引入的前提个人理解是为从外部读取局部变量,正常情况下,这是办不到的.简单的闭包举例如下: function f1(){ n=100; function f2(){ alert(n); } retu ...

  7. ASCII、Unicode、GBK和UTF-8字符编码的区别联系

    转自http://dengo.org/archives/901 很久很久以前,有一群人,他们决定用8个可以开合的晶体管来组合成不同的状态,以表示世界上的万物.他们看到8个开关状态是好的,于是他们把这称 ...

  8. win7 64的系统安装。net4.0总是提示安装未成功

    主要原因是Windows update的临时文件损坏,建议重命名该文件夹. 1. 开始——运行——cmd——键入net stop WuAuServ回车(停止windows update服务): 2. ...

  9. jquery双击事件

    <html> <head><meta http-equiv="Content-Type" content="text/html; chars ...

  10. 误打误撞写了段能让电脑奔溃的JS代码,但是自己不知道为什么,高手看到可以解答下吗?

    代码如下: <script> for(i=1;j=3*i;i++){ for(;j<=50;){ document.write(j+"<br>") } ...