By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
454,605 Members | 1,391 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 454,605 IT Pros & Developers. It's quick & easy.

1-17 in K&R book

P: n/a
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[i] = c;
if (c == '\n') {
s[i] = c;
++i;
}
s[i] = '\0';
return i;
}

void copyline(char to[], char from[])
{
int i;
i = 0;
while ((to[i] = from[i]) != '\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
----
line[10][MAXIMUM]
----
and hoped it would store 10 arrays but it doesn't seem to work in C.

What should I do?

Nov 16 '06 #1
Share this Question
Share on Google+
9 Replies


P: n/a
Camellia said:

<snip>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed,
<snip>
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.
Nov 16 '06 #2

P: n/a
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.
Richard Heathfield wrote:
Camellia said:

<snip>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed,
<snip>
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.
Nov 16 '06 #3

P: n/a
Camellia wrote:

Please don't top-post. Fixed here. Also trimmed deadwood.
Richard Heathfield wrote:
>Camellia said:

<snip>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed,
<snip>
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.

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.]

--
Chris "hantwig efferko VOOM!" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/

Nov 16 '06 #4

P: n/a
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.

On Nov 16, 11:09 pm, Chris Dollin <chris.dol...@hp.comwrote:
Camellia wrote:Please don't top-post. Fixed here. Also trimmed deadwood.
Richard Heathfield wrote:
Camellia said:
<snip>
The problem is my code doesn't store character arrays which means only
one longest string will be stored and printed,
<snip>
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.
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.]

--
Chris "hantwig efferko VOOM!" Dollin
"Life is full of mysteries. Consider this one of them." Sinclair, /Babylon 5/
Nov 16 '06 #5

P: n/a
Camellia wrote:
>
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.

On Nov 16, 11:09 pm, Chris Dollin <chris.dol...@hp.comwrote:
>Camellia wrote:Please don't top-post. Fixed here. Also trimmed deadwood.
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:
<news:news.announce.newusers
<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/>
Nov 16 '06 #6

P: n/a
CBFalconer wrote:
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.

Nov 16 '06 #7

P: n/a
CBFalconer wrote:
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.

Nov 16 '06 #8

P: n/a
On Thu, 2006-16-11 at 07:18 -0800, Camellia wrote:
CBFalconer wrote:
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.

--
Andrew Poelstra <http://www.wpsoftware.net>
For email, use [first_name].[last]@gmail.com
"You're only smart on the outside." -anon.

Nov 16 '06 #9

P: n/a
Camellia wrote:
CBFalconer wrote:
>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.

--
Chuck F (cbfalconer at maineline dot net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net>
Nov 17 '06 #10

This discussion thread is closed

Replies have been disabled for this discussion.