/* 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的更多相关文章

  1. C - The C Answer (2nd Edition) - Exercise 1-7

    /* Write a program to print the value of EOF. */ #include <stdio.h> main() { printf("EOF ...

  2. 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 ...

  3. C - The C Answer (2nd Edition) - Exercise 1-1

    /* Run the "hello, world" program on your system. Experiment with leaving out parts of the ...

  4. C - The C Answer (2nd Edition) - Exercise 1-8

    /* Write a program to count blanks, tabs, and newlines. */ #include <stdio.h> /* count blanks, ...

  5. 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 ...

  6. 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 ...

  7. 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 ...

  8. C - The C Answer (2nd Edition) - Exercise 1-4

    /* Write a program to print the corresponding Celsius to Fahrenheit table. */ #include <stdio.h&g ...

  9. 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 ...

随机推荐

  1. web前端开发小结

    1.浏览器内核 IE-----Trident Edge-----EdgeHTML Firefox-----Gecko Safari-----Webkit Chrome-----Blink(Webkit ...

  2. Wordpress入门笔记

    简单介绍一下wordpress个人操作,建议安装中文版. 登入后台管理者页面, 浏览器地址栏输入           (线上) http://XXXX.com/wp-login.php (本地) ht ...

  3. 80x86保护模式下IDT和中断调用过程分析

    80x86保护模式下IDT和中断调用过程分析 1.中断描述符表(IDT),将每个异常或中断向量分别与它们的处理过程联系起来.与GDT和LDT类似,IDT也是由8字节长度的描述符组成.IDT空描述符的存 ...

  4. 顺序表ans链性表

    #include<stdio.h>#include<malloc.h>#include<string.h>typedef int ElemType;typedef ...

  5. 在Notepad++里配置python环境

    首先在语言里选择Python 然后点击运行,在弹出的对话框里输入: cmd /k cd /d "$(CURRENT_DIRECTORY)" &  python " ...

  6. Python Mysql学习总结

    任何应用都离不开数据,所以在学习python的时候,当然也要学习一个如何用python操作数据库了.MySQLdb就是python对mysql数据库操作的模块.官方Introduction : MyS ...

  7. ubuntu安装jdk<服务器>

    服务器 阿里云服务器Ubuntu安装jdk7 2014-08-25 16:44 |  coding云 |  5825次阅读 | 6条评论   一.下载jdk 可以先下载到本地,然后ftp到服务器 官方 ...

  8. 【BJOI2014/bzoj4530】大融合

    题意 有 $n$ 个点,初始没有连边,要求支持两个动态操作: 1. 加一条边(保证之前两点不连通) 2. 查询过一条边的简单路径数量(就是两边连通块的大小的乘积) $n,Q\le 100000$ 题解 ...

  9. CountDownLatch和CyclicBarrier 的用法

    CountDownLatch是减计数方式,计数==0时释放所有等待的线程:CyclicBarrier是加计数方式,计数达到构造方法中参数指定的值时释放所有等待的线程.CountDownLatch当计数 ...

  10. npm install Unexpected token in JSON at position XXX

    问题描述执行npm install命令时出错,查看日志发现: Unexpected token < in JSON at position 183718 解决方法删除根目录下package-lo ...