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> ...
随机推荐
- mysql innodb 数据打捞(二)innodb 页面打捞编程
有了页面的结构和特征,需要编程实现数据库页面的打捞工作: 为了方便windows and linux 的通用,计划做成C语言的控制台应用,并且尽量只用ansi c;关于多线程,计划做成多线程的程序,最 ...
- [Guava官方文档翻译] 2.使用和避免使用null (Using And Avoiding Null Explained)
本文地址:http://www.cnblogs.com/hamhog/p/3536647.html "null很恶心." -Doug Lea "这是一个令我追悔莫及的错误 ...
- Java 使用反射拷贝对象一般字段值
在<Java解惑>上面看到第八十三例--诵读困难者,要求使用非反射实现单例对象的拷贝.查阅了部分资料,先实现通过反射拷贝对象. 1. 编写需要被拷贝的对象Person package co ...
- HTMLImageElement类型的简便利用
这个是我在复习书籍的时候看见的,当时一个同学想通过页面发送请求,但是数据量不是太大,所以用的get方式,但是页面用表单提交请求的话会让页面进行跳转,当时我在网上查了一点资料,发现基本上都是通过ajax ...
- C 字符/字符串常用函数
string.h中常用函数 char * strchr(char * str ,char ch); 从字符串str中查找首次出现字符ch的位置,若存在返回查找后的地址,若不存在则返回NULL void ...
- 安装mvc3出错致命错误
给vs2010安装mvc3,出现如下错误提示: Installation failed with error code: (0x80070643), "安装时发生严重错误 ". 将 ...
- Windows Phone 8 开发初体验
Windows Phone 8 是当前除了Android.IPhone之外,第3大智能手机运行平台.作为微软技术的忠实fans,一直关注和跟进微软技术的最新进展.这里就给大家简单介绍一下,如何进行Wi ...
- HTML5:基本使用
单行文本输入框 type为text表示input元素为一个单行文本框,是input元素的默认表现形式.单行文本输入框支持下面的属性设置. A:设定元素大小 maxlength属性设定用户能够输入的字符 ...
- rar压缩文件下载
//string fileName = "ceshi.rar";//客户端保存的文件名 //string filePath = Server.MapPath(&qu ...
- Bind Enum to ListControl
当使用MVVM时,相信你和我一样经常有这样的需求: 在ViewModel里定义了一个Enum,它必然是对应UI上的一个ListControl作为不同选项. 有一种做法是使用Converter,将Enu ...