problem with char array

P

pashmina g.

Hi,

I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string? I've tried to do
something like

char string[10];
....
char string2[20];
string = string2;

but it complains about incompatible types. And it wouldn't work either
if I use a 2 dimensional character array as the total length is still
fixed.

Thanks for help.


Pashmina
 
M

Martin Dickopp

I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?

I don't think you can without pointers. However, you don't need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.

Martin
 
N

nrk

Martin said:
I'm reading the book "The C programming Language". I'm trying to do an
exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?

I don't think you can without pointers. However, you don't need to store
a line, or even part of a line, at any time, if you only want to find
out which line is the longest.

The trick can be extended, so that you go through the file twice and print
the longest line without having to store any part of it.

-nrk.
 
P

pashmina g.

The exercise also asks to print out the text of the longest line. Those
lines are read from standard input, I shouldn't know anything about
reading nor writing a file yet. In fact I've no idea of how to do that
assuming that I should use only those things taught in previous chapter
(variables and arithmetic expressions, for, symbolic constant, character
input/output, array, function and character arrays).



Pashmina



I'm reading the book "The C programming Language". I'm trying to do
an exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?

I don't think you can without pointers. However, you don't need to
store a line, or even part of a line, at any time, if you only want to
find out which line is the longest.

Martin


--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-.
=.
/ ,- ) http://www.zero-based.org/ ((_/)o
o(\_))\ `-'
`-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/


--
 
R

Richard Heathfield

pashmina said:
The exercise also asks to print out the text of the longest line.

If this is Exercise 1-16, note the words "and as much as possible of the
text". This gives you the freedom to store only up to MAXLEN (define this
as you wish) bytes of the line.

Suffice to say that it /is/ possible to store arbitrarily long strings in C,
but to do that (at least, in a portable and robust way) requires knowledge
which you have not yet acquired in Chapter 1 of K&R2.

<snip>
 
M

Martin Dickopp

pashmina g. said:
I'm reading the book "The C programming Language". I'm trying to do
an exercise asking to find the longest line. I'm reading the book in
sequence hence I shouldn't know anything about pointer yet.

As a string is declared with "char string[n]", the length is already
specified, how can I store an arbitrary long string?

I don't think you can without pointers. However, you don't need to
store a line, or even part of a line, at any time, if you only want to
find out which line is the longest.

The exercise also asks to print out the text of the longest line. Those
lines are read from standard input, I shouldn't know anything about
reading nor writing a file yet. In fact I've no idea of how to do that
assuming that I should use only those things taught in previous chapter
(variables and arithmetic expressions, for, symbolic constant, character
input/output, array, function and character arrays).

Please don't top-post (fixed), and please don't quote irrelevant parts
like signatures.

If you cannot use pointers (which means you cannot use dynamic memory
allocation), and if you cannot assume a fixed upper bound on the line
length, there's no solution which works for any input.

I suggest that you write a program which works if no line is longer
than, say, 255 characters. However, the program should /not/ assume that
that is true for the input provided to it. If it encounters a longer
line, it should print a meaningful error message. Alternatively, you
could write it so that it only prints the first 255 characters of the
longest line if that line is longer than that.

Martin
 
G

Guillaume

The trick can be extended, so that you go through the file twice and print
the longest line without having to store any part of it.

You don't have to go through the file twice.

You can have a byte index for the start of the line and the line length,
both stored in two size_t variables.

After going through the file once, you'll have a valid index and you can
use fseek() and fread() to read the line content. It's not quite like
"going through the file twice". ;-)
 
C

CBFalconer

Guillaume said:
You don't have to go through the file twice.

You can have a byte index for the start of the line and the line
length, both stored in two size_t variables.

After going through the file once, you'll have a valid index and you
can use fseek() and fread() to read the line content. It's not quite
like "going through the file twice". ;-)

Depending on the file and system, you cannot always seek etc. The
only safe method is to use something like ggets (available on my
page) and have two buffered lines available - the current one and
the longest so far. Now the only assumption needed is that you
can read the file once up to EOF.
 
G

Guillaume

Depending on the file and system, you cannot always seek etc. The
only safe method is to use something like ggets (available on my
page) and have two buffered lines available - the current one and
the longest so far. Now the only assumption needed is that you
can read the file once up to EOF.

True, and with the standard input, fseek() is not an option, nor is
going backwards...
 
S

Sriram

Hi,
char longestline [1000] ; /* The Longest Line i expect */

DONT allocate memory at run time until it is obsolutely neccessary.

If u know what a buffer size must be maximum. Fix it and use it.



Regards,
Ram
 
D

Darklight

this answer comes from
the c answer book by tondo & gimpel
the only revision to the main routine is
printf("%d, %s",len,line);
ill let you work out were to put that

you should change the getline function to

int getline(char s[], int lim)
{
int c, i, j:

j = 0;
for(i = 0; (c = getchar()) != EOF && 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;
}
 
J

Joe Wright

Darklight said:
this answer comes from
the c answer book by tondo & gimpel
the only revision to the main routine is
printf("%d, %s",len,line);
ill let you work out were to put that

you should change the getline function to

int getline(char s[], int lim)
{
int c, i, j:

j = 0;
for(i = 0; (c = getchar()) != EOF && 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;
}

Yours has a typo and bad indenting. How about..

int getline(char s[], int lim)
{
int c, i, j;
j = 0;
for (i = 0; (c = getchar()) != EOF && 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;
}
 

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

Members online

Forum statistics

Threads
474,310
Messages
2,571,603
Members
48,419
Latest member
EstelaCout

Latest Threads

Top