473,395 Members | 1,631 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,395 software developers and data experts.

problem with char array

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
Nov 14 '05 #1
11 1600
pa*******@interfree.it (pashmina g.) writes:
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. \_/
Nov 14 '05 #2
nrk
Martin Dickopp wrote:
pa*******@interfree.it (pashmina g.) writes:
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.
Martin


--
Remove devnull for email
Nov 14 '05 #3
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

On Thu, 26 Feb 2004 13:14:45 +0100
Martin Dickopp <ex****************@zero-based.org> wrote:
pa*******@interfree.it (pashmina g.) writes:
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. \_/

--
Nov 14 '05 #4
"pashmina g." <pa*******@interfree.it> writes:
On Thu, 26 Feb 2004 13:14:45 +0100
Martin Dickopp <ex****************@zero-based.org> wrote:
pa*******@interfree.it (pashmina g.) writes:
> 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
--
,--. Martin Dickopp, Dresden, Germany ,= ,-_-. =.
/ ,- ) http://www.zero-based.org/ ((_/)o o(\_))
\ `-' `-'(. .)`-'
`-. Debian, a variant of the GNU operating system. \_/
Nov 14 '05 #5
pashmina g. wrote:
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>

--
Richard Heathfield : bi****@eton.powernet.co.uk
"Usenet is a strange place." - Dennis M Ritchie, 29 July 1999.
C FAQ: http://www.eskimo.com/~scs/C-faq/top.html
K&R answers, C books, etc: http://users.powernet.co.uk/eton
Nov 14 '05 #6
> 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". ;-)

Nov 14 '05 #7
Guillaume wrote:
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". ;-)


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.

--
Chuck F (cb********@yahoo.com) (cb********@worldnet.att.net)
Available for consulting/temporary embedded and systems.
<http://cbfalconer.home.att.net> USE worldnet address!
Nov 14 '05 #8
> 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...

Nov 14 '05 #9
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
Nov 14 '05 #10
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;
}

Nov 14 '05 #11
Darklight wrote:

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;
}

--
Joe Wright http://www.jw-wright.com
"Everything should be made as simple as possible, but not simpler."
--- Albert Einstein ---
Nov 14 '05 #12

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

4
by: Krzysztof | last post by:
Hi! I have a class: some_class{ private: char* seq; public: some_class(char* se); char* ret_seq(); ....
28
by: Davy | last post by:
Hi all, I found char x={"my"}; can be compiled. But char x; x={"my"}; can not be compiled.
6
by: Edd Dawson | last post by:
Hi. I have a strange problem involving the passing of command line arguments to a C program I'm writing. I tried posting this in comp.programming yesterday but someone kindly suggested that I'd...
3
by: ritchie | last post by:
Hi all! Still working on this program! Just to recap, I am writing a program to sort an array with four different sort algorythms. I am having a little trouble at the moment though! Now, I...
8
by: intrepid_dw | last post by:
Hello, all. I've created a C# dll that contains, among other things, two functions dealing with byte arrays. The first is a function that returns a byte array, and the other is intended to...
2
by: ajikoe | last post by:
Hi, I tried to follow the example in swig homepage. I found error which I don't understand. I use bcc32, I already include directory where my python.h exist in bcc32.cfg. /* File : example.c...
39
by: Martin Jørgensen | last post by:
Hi, I'm relatively new with C-programming and even though I've read about pointers and arrays many times, it's a topic that is a little confusing to me - at least at this moment: ---- 1)...
12
by: NOO Recursion | last post by:
Hi everyone! I am trying to write a program that will search a 12x12 for a thing called a "blob". A blob in the grid is made up of asterisks. A blob contains at least one asterisk. If an...
5
by: weidongtom | last post by:
Hi, I tried to implement the Universal Machine as described in http://www.boundvariable.org/task.shtml, and I managed to get one implemented (After looking at what other's have done.) But when I...
9
by: weidongtom | last post by:
Hi, I've written the code that follows, and I use the function add_word(), it seems to work fine *before* increase_arrays() is called that uses realloc() to allocate more memory to words. But...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
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...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
0
BarryA
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...
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
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...
0
jinu1996
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...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.