1-17 in K&R book

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
 
R

Richard Heathfield

Camellia said:

The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed,
What should I do?

Print as you go. That is, once you've decided whether a line is longer than
twenty characters and therefore needs to be printed, print it immediately,
before reading the next line.

--
Richard Heathfield
"Usenet is a strange place" - dmr 29/7/1999
http://www.cpax.org.uk
email: normal service will be restored as soon as possible. Please do not
adjust your email clients.
 
C

Camellia

Thanks so much for the input.

Yeah I thought so but I just wonder is there a possible method to store
the strings(>20 characters) and print them out at last? Because that
seems to be what the question want.
 
C

Chris Dollin

Camellia wrote:

Please don't top-post. Fixed here. Also trimmed deadwood.
Thanks so much for the input.

Yeah I thought so but I just wonder is there a possible method to store
the strings(>20 characters) and print them out at last? Because that
seems to be what the question want.

You said the exercise said:

| Write a program to print all input lines that are longer than 20
| characters

Nothing there about having to store stuff and print it later.

[Yes, you /can/ store long strings and print them later, assuming
there's enough room. But you don't /need/ to for this exercise.
And that means you don't have to get tied up in the behaviour
of malloc/free/realloc. Deep Joy.]
 
C

Camellia

I just re-read the question and yes, I don't seem to have to do that:)
The K&R book will cover it later I suppose, right?

Thank you for the reply.

Camellia wrote:please don't top-post. Fixed here. Also trimmed deadwood.


Thanks so much for the input.
Yeah I thought so but I just wonder is there a possible method to store
the strings(>20 characters) and print them out at last? Because that
seems to be what the question want.You said the exercise said:

| Write a program to print all input lines that are longer than 20
| characters

Nothing there about having to store stuff and print it later.

[Yes, you /can/ store long strings and print them later, assuming
there's enough room. But you don't /need/ to for this exercise.
And that means you don't have to get tied up in the behaviour
of malloc/free/realloc. Deep Joy.]
 
C

CBFalconer

Camellia said:
I just re-read the question and yes, I don't seem to have to do
that:) The K&R book will cover it later I suppose, right?

Thank you for the reply.

Once more, don't top-post. If you continue to do so you will
simply be ignored by many. See the links below.

--
Some informative links:
< <http://www.geocities.com/nnqweb/>
<http://www.catb.org/~esr/faqs/smart-questions.html>
<http://www.caliburn.nl/topposting.html>
<http://www.netmeister.org/news/learn2quote.html>
<http://cfaj.freeshell.org/google/>
 
C

Camellia

CBFalconer said:
Once more, don't top-post. If you continue to do so you will
simply be ignored by many.
[snip: links]

Oh I'm so sorry I didn't even what's top-posting until I read the
links, thank you for pointing it out.
 
C

Camellia

CBFalconer said:
Once more, don't top-post. If you continue to do so you will
simply be ignored by many.

[snip: links]

Oh I'm so sorry I didn't even know what's top-posting until I read the
links, thank you for pointing it out.
 
A

Andrew Poelstra

CBFalconer said:
Once more, don't top-post. If you continue to do so you will
simply be ignored by many.
[snip: links]

Oh I'm so sorry I didn't even what's top-posting until I read the
links, thank you for pointing it out.

You didn't have to snip the links; they'll appear in many archives no
matter what you do. It's not a big deal.

Just for fun, let's compare what you said to the typical response:

You: "Oh I'm so sorry I didn't even what's top-posting until I read the
links, thank you for pointing it out."
Most: "its a public newsgroup dont tell me what to do u ****** i can do
whatever the **** i want and u cant do nething abt it"


You're welcome here.
 
C

CBFalconer

Camellia said:
CBFalconer said:
Once more, don't top-post. If you continue to do so you will
simply be ignored by many.
[snip: links]

Oh I'm so sorry I didn't even what's top-posting until I read the
links, thank you for pointing it out.

Good for you. Looks like you can become a worthy addition to
usenet.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Similar Threads

K&R 1-24 15
K$R xrcise 1-13 (histogram) 4
K&R Exercise 1-21: entab 10
K&R exercise 1-18 10
K&R xrcise 1-13 6
K&R p.97 8
How to build a char[][] in k&r dialect. 7
compile error 30

Members online

No members online now.

Forum statistics

Threads
473,770
Messages
2,569,584
Members
45,075
Latest member
MakersCBDBloodSupport

Latest Threads

Top