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 ...
随机推荐
- python学习-- {% csrf_token %}
1.不推荐禁用掉django中的CSRF. 2.我们可以再html页面的form表单中添加csrf_token,带着表单的请求一起发送到服务器去验证. <form enctype=" ...
- Timus 1329. Galactic History。LCA最近公共祖先或dfs递归离线处理!
1329. Galactic History 比赛的时候看到学弟A了这题然后跟榜做,结果在LCA的道路上一去不复返,这个题是很像LCA求最近公共祖先的,不过三个人都没学过LCA,只能拿着资料看着像然后 ...
- Android线程中使用Toast、dialog、loading
代码改变世界 Android线程中使用Toast.dialog.loading Loading: Thread t1 = new Thread(new Runnable() { @Override p ...
- win 7 取得最高权限
以管理员身份运行cmd,然后输入: net user administrator /active:yes 然后注销,就会看到你原来的用户已经是最高权限的用户了.以后做的操作都是最高权限的操作.
- 刷题总结——运输计划(bzoj4326)
题目: 题目背景 NOIP2015 提高组 Day2 T3 题目描述 公元2044年,人类进入了宇宙纪元. L国有 n 个星球,还有 n-1 条双向航道,每条航道建立在两个星球之间,这 n-1 条航道 ...
- 北京集训TEST12——PA( Mortal Kombat)
题目: Description 有一天,有N个外星人企图入侵地球.地球派出全球战斗力最强的M个人代表人类对抗外星人.根据外星的战斗规则,每个外星人应该分别与一名地球人对战(不同的外星人要与不同的地球人 ...
- Microsoft IIs tilde directory enumeration
漏洞标题: iis 短文件名列举漏洞 检测: https://code.google.com/p/iis-shortname-scanner-poc/ 查看扫描出来的目录,全是404 ,比 ...
- axis2生成webservice服务端返回String[]和String[][]一维数组和二维数组解析
环境:用axis2生成服务端,用aixs做客户端 1:直接返回String[]: public String[] testArr(String name) { String[] ret=new Str ...
- 莫比乌斯函数之和(51nod 1244)
莫比乌斯函数,由德国数学家和天文学家莫比乌斯提出.梅滕斯(Mertens)首先使用μ(n)(miu(n))作为莫比乌斯函数的记号.具体定义如下: 如果一个数包含平方因子,那么miu(n) = 0.例如 ...
- Problem 2111 Min Number
...