the c language练习 1-16

#include <stdio.h>
#include<stdlib.h>
#define MAXLINE 1000 /* maximum input line length */
//练习 1-16 修改打印最长文本行的程序的主程序 main,使之可以打印任意长度的输入
//行的长度,并尽可能多地打印文本。
int getline(char line[], int maxline);
void copy(char to[], char from[], int len);
/* print the longest input line */
int main()
{
    int len; /* current line length,当前行的长度 */
    int max; /* maximum length seen so far,最大长度 */
    int c;
    char line[MAXLINE]; /* current input line ,当前的输入行*/
    char longest[MAXLINE]; /* longest line saved here最长的输入行 */
    max = 0;
    int maxlength = 0;
    while ((len = getline(line, MAXLINE)) > 0) {
        copy(longest, line, len);
        //  printf("%s", longest);
        maxlength = maxlength + len;
    }
    for (int i = 0; i < maxlength; i++) {
            printf("%c", longest[i]);
    }

    system("pause");

    return 0;
}
/* getline: read a line into s, return length */
int getline(char s[], int lim)
{
    int c, i;
    for (i = 0; i < lim - 1 && (c = getchar()) != EOF && c != '\n'; ++i)
        s[i] = c;
    if (c == '\n') {
        s[i] = c;
        ++i;
    }
    return i;
}
/* copy: copy 'from' into 'to'; assume to is big enough */
void copy(char to[], char from[], int len)
{
    static int i = 0;
    int b = i + len;
    for (int j = 0; i < b; ++i, j++) {
        to[i] = from[j];
    }
    // ++i;
}
 

猜你喜欢

转载自blog.csdn.net/fuhuangjjj/article/details/89037133