小弟刚刚入门,请高人指点,我的邮箱:[email=longyuping1985@126.com]longyuping1985@126.com[/email]
以下是源代码:
#include
#define MAXLINE 1000 /* maximum input line size */
int geline(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]; /* save the longest line current */
max = 0;
while ((len = getline(line, MAXLINE)) >= 0) {
printf("%d, %s", len, line);
if (len > max) { /* select the max line */
max = len;
copy(longest, line);
}
}
/* 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()) != '?' && 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 = from[i]) != '\0')
++i;
}[/i]
longyu1985 于 2010-01-19 11:08:12发表:
2# longyu1985
最好那里是:while ((to[i] = from[i]) != '\0')
longyu1985 于 2010-01-19 11:04:32发表:
1# longyu1985
源代码:#include
#define MAXLINE 1000 /* maximum input line size */
int geline(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]; /* save the longest line current */
max = 0;
while ((len = getline(line, MAXLINE)) >= 0) {
printf("%d, %s", len, line);
if (len > max) { /* select the max line */
max = len;
copy(longest, line);
}
}
/* 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()) != '?' && 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;
}