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? 9 1357
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.
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.
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/
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/
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/>
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.
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.
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.
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> This thread has been closed and replies have been disabled. Please start a new discussion. Similar topics
by: anonymous |
last post by:
Hi CLCers,
I want to know your opinion about the book: Expert C
programming-Deep C secrets by Peter Van Der Linden. Thanks
in advance.
Sha
|
by: Guido Mureddu |
last post by:
Hello,
I'm a student in electronic engineering. I do know you've seen and answered this
sort of topic/request countless times, but I haven't found past threads as
helpful as I had hoped, and...
|
by: Robert Zurer |
last post by:
Can anyone suggest the best book or part of a book on this subject. I'm
looking for an in-depth treatment with examples in C#
TIA
Robert Zurer
robert@zurer.com
|
by: Matt Wiggins |
last post by:
I'm new to vb.net, but I've been using vb for a couple of years. Can anyone
recommend a good book to learn vb.net?
Thanks
--
Matt
|
by: www.douglassdavis.com |
last post by:
I'm looking for advice here, and I would really appreciate it if you
could help.
Is there a VB 2005 book that you like and would recommend (and why)?
Would you consider it good for...
|
by: Malcolm McLean |
last post by:
The webpages for my new book are now up and running.
The book, Basic Algorithms, describes many of the fundamental algorithms
used in practical programming, with a bias towards graphics. It...
|
by: Hello |
last post by:
Hello every body
Please can any body tells me a good book that can teach me "visual basic 2005" (as beginner).
Thank you all
=========================================
|
by: Javilen |
last post by:
Hello,
edit: added some more info
I am trying to set up a job to run a SSIS package in SQL 2005, the SQL 2005 is installed on a Windows server 2003 machine.
start Edit
I have the...
|
by: jrw133 |
last post by:
i got this program the other day and ive just started it and i am getting some errors that i cant figure out.
requirements: 1)create a clas called Book. a Book has three data members: m_title,...
|
by: Charles Arthur |
last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
|
by: ryjfgjl |
last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
|
by: BarryA |
last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
|
by: Sonnysonu |
last post by:
This is the data of csv file
1 2 3
1 2 3
1 2 3
1 2 3
2 3
2 3
3
the lengths should be different i have to store the data by column-wise with in the specific length.
suppose the i have to...
|
by: marktang |
last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However,...
|
by: Hystou |
last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
|
by: Oralloy |
last post by:
Hello folks,
I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>".
The problem is that using the GNU compilers,...
|
by: jinu1996 |
last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
|
by: agi2029 |
last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
| |