#include <stdio.h>
#define MAXLEN 10
#define SP ' '
#define TAB '\t'
int max = 0;
char line[MAXLEN];
char longest[MAXLEN];
int getline(char *, int);
void copy(char *, char *);
void removeChar(char *, int maxlen);
int main(int argc, char **argv) {
int len = 0;
int max = 0;
while ((len = getline(line, MAXLEN)) > 0) {
removeChar(line, MAXLEN);
if (len > max) {
copy(line, longest);
max = len;
}
}
if(max > 0 ) {
printf("longest line: %s\n", longest);
}
return 0;
}
int getline(char *des, int limit) {
char c;
int i = 0;
while ((c = getchar()) != EOF && c != '\n' && --limit >= 0) {
des[i++] = c;
}
if (c == '\n') {
des[i++] = c;
}
des[i] = '\0';
return i;
}
void copy(char *src, char *des) {
int i = 0;
while ((des[i] = src[i]) != '\0') {
++i;
}
return;
}
void removeChar(char *line, int maxLen) {
int i = maxLen - 1;
while (line[i] == SP || line[i] == TAB) {
--i;
}
line[++i] = '\0';
return;
}
1-18 练习
C语言读取最长行
最新推荐文章于 2024-12-11 16:41:16 发布
2418

被折叠的 条评论
为什么被折叠?



