C
Camellia
hi all
I'm working on the exercise 1-17 in the K&R book, it says:
-------------
Write a program to print all input lines that are longer than 20
characters
-------------
and this is my result:
<code>
#include <stdio.h>
#define MINIMUM 20
#define MAXIMUM 1000
int getline(char s[], int limit);
void copyline(char to[], char from[]);
main()
{
int len;
char line[MAXIMUM];
char line_20[MAXIMUM];
int max = 0;
while ((len = getline(line, MAXIMUM)) > 0)
if (len >= MINIMUM) {
max = len;
copyline(line_20, line);
}
if (max >= MINIMUM)
printf("%s", line_20);
return 0;
}
int getline(char s[], int limit)
{
int i;
int c;
for (i = 0; (c = getchar()) != EOF && c != '\n' && i < limit-1;
++i)
s = c;
if (c == '\n') {
s = c;
++i;
}
s = '\0';
return i;
}
void copyline(char to[], char from[])
{
int i;
i = 0;
while ((to = from) != '\0')
++i;
}
</code>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed, how do I store strings
long than 20 characters? I tried
I'm working on the exercise 1-17 in the K&R book, it says:
-------------
Write a program to print all input lines that are longer than 20
characters
-------------
and this is my result:
<code>
#include <stdio.h>
#define MINIMUM 20
#define MAXIMUM 1000
int getline(char s[], int limit);
void copyline(char to[], char from[]);
main()
{
int len;
char line[MAXIMUM];
char line_20[MAXIMUM];
int max = 0;
while ((len = getline(line, MAXIMUM)) > 0)
if (len >= MINIMUM) {
max = len;
copyline(line_20, line);
}
if (max >= MINIMUM)
printf("%s", line_20);
return 0;
}
int getline(char s[], int limit)
{
int i;
int c;
for (i = 0; (c = getchar()) != EOF && c != '\n' && i < limit-1;
++i)
s = c;
if (c == '\n') {
s = c;
++i;
}
s = '\0';
return i;
}
void copyline(char to[], char from[])
{
int i;
i = 0;
while ((to = from) != '\0')
++i;
}
</code>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed, how do I store strings
long than 20 characters? I tried