让程序读入一组文本行,并打印最长的一行
getline函数理解
#include <stdio.h>
int c, i;
const int n = 99;
char l[n];
main() {
//处理一行
for (i = 0; (c = getchar()) != EOF && c != '\n'; ++i) {
l[i] = c;
printf("i=%d\t1=%c\n", i, l[i]);
}
printf("%d\n", i);
if (c == '\n') {
l[i] = c;
printf("i=%d\t2=%c\n", i, l[i]);
i++;
}
l[i] = '\0';
printf("i=%d\t3=%c", i, l[i]);
return i;
}
随便输入
总函数
#include <stdio.h>
#define MAXL 1000
char line[MAXL];
char lgst[MAXL];
int getline(char line[]);
void copy(char lgst[], char line[]);
main() {
int now, t;
int max = 0;
/* while ((t = getline(line) ) > max) {
max = t;
copy(lgst, line);
} 这是第一次犯的错误,第一次取得最大值就直接返回了*/
while ((t = getline(line)) > 0) {//括号!没括号只返回第一行
if (t > max) {
max = t;
copy(lgst, line); //都是字符数组,临时保存输入行的字符
}
}
if (max > 0)
printf("biiiiiiiiiiiigstis:%s", lgst);
}
int getline(char l[]) { //处理一行
int c, i;
for (i = 0; (c = getchar()) != EOF && c != '\n'; i++)
l[i] = c;
if (c == '\n') {
l[i] = c;
i++;
}
l[i] = '\0';
return i;
}
void copy(char cp[], char orig[]) {
int i = 0;
while (orig[i] != '\0') {
cp[i] = orig[i];
i++;
}
}