C - The C Answer (2nd Edition) - Exercise 1-16
/* 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. */ #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 */
main()
{
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 = 0;
while((len = getline(line, MAXLINE)) > 0)
{
printf("%d, %s", len, line);
if(len > max)
{
max = len;
copy(longest, line);
}
}
if(max > 0) /* there was a line */
{
printf("%s", longest);
}
return 0;
} /* getline: read a line into s, return length */
int getline(char s[], int lim)
{
int c, i, j;
j = 0;
for(i = 0; (c = getchar()) != EOF && c != '\n'; ++i)
{
if(i < lim - 2)
{
s[j] = c; /* line still in boundaries */
++j;
}
}
if(c == '\n')
{
s[j] = c;
++j;
++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 = 0;
while((to[i] = from[i]) != '\0')
{
++i;
}
}
C - The C Answer (2nd Edition) - Exercise 1-16的更多相关文章
- C - The C Answer (2nd Edition) - Exercise 1-7
/* Write a program to print the value of EOF. */ #include <stdio.h> main() { printf("EOF ...
- C - The C Answer (2nd Edition) - Exercise 1-2
/* Experiment to find out what happens when printf's argument string contains \c, where c is some ch ...
- C - The C Answer (2nd Edition) - Exercise 1-1
/* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...
- C - The C Answer (2nd Edition) - Exercise 1-8
/* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, ...
- C - The C Answer (2nd Edition) - Exercise 1-5
/* Modify the temperature conversion program to print the table in reverse order, that is, from 300 ...
- C - The C Answer (2nd Edition) - Exercise 1-15
/* Rewrite the temperature conversion program of Section 1.2 to use a function for conversion. */ #i ...
- C - The C Answer (2nd Edition) - Exercise 1-12
/* Write a program that prints its input one word per line. */ #include <stdio.h> #define IN 1 ...
- C - The C Answer (2nd Edition) - Exercise 1-4
/* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h&g ...
- C - The C Answer (2nd Edition) - Exercise 1-6
/* Verify that the expression getchar() != EOF is 0 or 1. */ #include <stdio.h> main() { int c ...
随机推荐
- Cookie和Session的作用和工作原理
一.Cookie详解 (1)简介 因为HTTP协议是无状态的,即服务器不知道用户上一次做了什么,这严重阻碍了交互式Web应用程序的实现.在典型的网上购物场景中,用户浏览了几个页面,买了一盒饼干和两饮料 ...
- Leetcode 467.环绕字符串中的唯一子字符串
环绕字符串中的唯一子字符串 把字符串 s 看作是"abcdefghijklmnopqrstuvwxyz"的无限环绕字符串,所以 s 看起来是这样的:"...zabcdef ...
- Linux免密远程登陆
上一节讲到伪分布式部署,启动后需要输入4次密码,停止服务后也要输入4次密码.本节记录免密登陆原理和实践 假设有2台服务器(A和B)(这是配置原理) 1)A需要远程登录B服务器,那么A就要创建密钥对(私 ...
- WebService的简介, 原理, 使用,流程图
WebService的简介, 原理, 使用 第一部分: 直观概述 WebService的几种概念: 以HTTP协议为基础,通过XML进行客户端和服务器端通信的框架/组件 两个关键点: 1. ...
- Docker网络基础:快速指南
Docker网络基础:快速指南 原文连接:http://blogxinxiucan.sh1.newtouch.com/2017/07/30/Docker网络基础:快速指南/ 了解有关扩展网络功能的默认 ...
- 设计模式(四)建造者模式 Builder
Builder: <Effective Java> 第2条:遇到多个构造器参数时要考虑用构建器. 建造者模式(Builder Pattern),也称生成器模式,定义如下: 将一个复杂对象的 ...
- 阿狸的打字机(bzoj 2434)
Description 阿狸喜欢收藏各种稀奇古怪的东西,最近他淘到一台老式的打字机.打字机上只有28个按键,分别印有26个小写英文字母和'B'.'P'两个字母. 经阿狸研究发现,这个打字机是这样工作的 ...
- 定情信物(bzoj 3823)
Description 都说程序员找不到妹子,可是无人知晓,三生石上竟然还刻着属于小 E 的一笔. 那一天,小 E 穷尽毕生的积蓄,赠与了妹子一个非同寻常的定情信物.那是一个小 小的正方体,但透过它, ...
- Spring定义的五种事务隔离级别
在Spring中定义了5中不同的事务隔离级别. 1. ISOLATION_DEFAULT(一般情况下使用这种配置既可) 这是一个PlatfromTransactionManager默认的隔离级别,使用 ...
- SpringCloud-分布式配置中心【入门介绍】
案例代码:https://github.com/q279583842q/springcloud-e-book 一. 为什么需要使用配置中心 1 服务配置的现状 2 常用的配置管理解决方案的缺点 3 为 ...