利用c语言找出输入文本最长的一行
生活随笔
收集整理的這篇文章主要介紹了
利用c语言找出输入文本最长的一行
小編覺得挺不錯的,現(xiàn)在分享給大家,幫大家做個參考.
前言
來源之《c編程語言》練習(xí)1-16
代碼
#include<stdio.h>
#define MAXLINE 1000int getline(char line[], int maxline);
void copy(char to[], char from[]);int main()
{int len, max;char line[MAXLINE];char longest[MAXLINE];max = 0;while ((len = getline(line, MAXLINE)) > 0){printf("%d, %s", len, line);if (len > max){max = len;copy(longest, line);}}if (max > 0){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;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;}
}
總結(jié)
以上是生活随笔為你收集整理的利用c语言找出输入文本最长的一行的全部內(nèi)容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: python中的match和search
- 下一篇: c/c++中的函数指针和指针函数