c程序设计语言_习题1-16_自己编写getline()函数,接收整行字符串,并完整输出
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()函数,接收整行字符串,并完整输出的更多相关文章
- c程序设计语言_习题7-6_对比两个输入文本文件_输出它们不同的第一行_并且要记录行号
Write a program to compare two files, printing the first line where they differ. Here's Rick's solut ...
- c程序设计语言_习题8-4_重新实现c语言的库函数fseek(FILE*fp,longoffset,intorigin)
fseek库函数 #include <stdio.h> int fseek(FILE *stream, long int offset, int origin); 返回:成功为0,出错 ...
- c程序设计语言_习题8-6_利用malloc()函数,重新实现c语言的库函数calloc()
The standard library function calloc(n,size) returns a pointer to n objects of size size , with the ...
- 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 ...
- c程序设计语言_习题1-18_删除输入流中每一行末尾的空格和制表符,并删除完全是空格的行
Write a program to remove all trailing blanks and tabs from each line of input, and to delete entire ...
- 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 ...
- c程序设计语言_习题1-11_学习单元测试,自己生成测试输入文件
How would you test the word count program? What kinds of input are most likely to uncover bugs if th ...
- c程序设计语言_习题1-9_将输入流复制到输出流,并将多个空格过滤成一个空格
Write a program to copy its input to its output, replacing each string of one or more blanks by a si ...
- 《c程序设计语言》读书笔记-4.2-扩充atof函数
#include <stdio.h> #include <string.h> #include <stdlib.h> #include <math.h> ...
随机推荐
- 第11条:谨慎地覆盖clone
Clone提供一种语言之外的机制:无需调用构造器就可以创建对象. 它的通用约定非常弱: 创建和返回该对象的一个拷贝.这个拷贝的精确含义取决于该对象的类.一般含义是,对于任何对象x,表达式x.clone ...
- ubuntu12.10可用更新源
ubutnu12.10自带的更新源已经失效,国内各大服务器的更新源,无论是网易.搜狐还是教育网的因此也失效了.附件是ubuntu12.10目前的可用更新源. 将地址中的“us.archive”换成“o ...
- React:用于搭建UI的JavaScript库
React https://facebook.github.io/react/index.html 2016-08-03 先吐槽一下.看过很多博客.教程.文章,一直想不通为什么大牛们介绍一种新技术一上 ...
- RX学习笔记:JavaScript数组操作
RX学习笔记:JavaScript数组操作 2016-07-03 增删元素 unshift() 在数组开关添加元素 array.unshift("value"); array.un ...
- html5在手机端关于 map area中的自适应
https://github.com/stowball/jQuery-rwdImageMaps用这一个插件可自适应!!!
- css部分基础归纳--学习笔记
(1)css不区别大小写: (2)颜色值:颜色值可以写成RGB格式,如:color:rgb(255,100,0),也可以写成十六进制格式,如:color:#ff0000.如果十六进制的值是成对重复的可 ...
- Delphi IDE下载全地址
Delphi IDE下载全地址: http://pan.baidu.com/share/home?uk=1060104307#category/type=0 还是网友伟大呀.当然有钱的公司还是应该多多 ...
- pycharm3.x 注册码
PyCharm 3.0 注册码 PyCharm3 序列号 License Key 用户名:yueting3527 注册码: ===== LICENSE BEGIN ===== 93347-120420 ...
- hdu 5648 DZY Loves Math 组合数+深搜(子集法)
题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=5648 题意:给定n,m(1<= n,m <= 15,000),求Σgcd(i|j,i&am ...
- entity framework extended library , bulk execute,deleting and updating ,opensource
http://weblogs.asp.net/pwelter34/entity-framework-batch-update-and-future-queries