Groups | Search | Server Info | Keyboard shortcuts | Login | Register [http] [https] [nntp] [nntps]
| From | Ben Bacarisse <ben.usenet@bsb.me.uk> |
|---|---|
| Newsgroups | comp.lang.c |
| Subject | Re: Program to extract a substring from a string; compiling issues |
| Date | 2015-10-14 21:22 +0100 |
| Organization | A noiseless patient Spider |
| Message-ID | <871tcx9p4o.fsf@bsb.me.uk> (permalink) |
| References | <ddd5630d-29e4-4664-9f19-06aaf72dce6c@googlegroups.com> <87y4f5aafc.fsf@bsb.me.uk> <df41466b-e5e8-4182-abf3-a59ae10b58b8@googlegroups.com> |
Alla _ <modelling.data@gmail.com> writes:
>> > subword [] = substring (word, start_letter, count_number);
>>
>> You can't assign to an array in C, if that is what you are trying to do
>> (that would look more like subword = substring(...) so maybe that's not
>> what you are trying).
>>
>> Pretty much the only viable option here is to pass into substring a
>> pointer to the (start of) the array into which the substring should be
>> copied.
>>
> Unfortunately, I don't understand this part. I don't see
> how this functions differs from the function in the below
> program (copied from the book), which doesn't return
> anything as well.
<snip>
> void read_line (char buffer [], int size);
Indeed not. This function does exactly what I'm saying you need to do.
Here, buffer is a pointer to the start of an array of char. The
function fill the array with data read from the input stream.
You substring function needs another parameter which will be a pointer
to the array where it should copy the character of the requested
sub-string.
<snip>
>> > void substring (char source[], int start, int count)
>> > {
>> > int i;
>> > char result [80];
>>
>> Why 80? You can't know know that 80 is enough, but if you've picked a
>> size for an array, make sure you check that it is enough. Either refuse
>> to do anything if count >= 80, or only put a maximum of 79 characters
>> and a null into it.
>>
> The book says that 80 is a standard width of the terminal :)
> Actually, 80 is my mistake, corrected it to 81 to account for
> a null character.
OK, so you've explained why you pick that number (and I knew that too to
be honest -- it was a rhetorical question) but do you understand the
rest? It's very important to know why it's wrong to ignore an array's
size.
>
>> > if (count > string_length (source))
>>
>> This is not a useful test. See below.
>>
>> > {
>> > for (i = start; i <= (start + count) && source[i] != '\0'; i++)
>> > {
>> > result [i] = source [i];
>>
>> So if start is 17, i starts at 17 and this does result[17] =
>> source[17]. Is that what you want?
>>
>> > }
>> > result [i + 1] = '\0';
>> > }
>> > else
>> > {
>>
>> Here we know that count <= string_length(source) but that's not really
>> very useful to know. If the length is 100 and start is 99 even a count
>> of 10 goes wrong.
>>
>>
> Corrected :)
> int i;
> char result [81];
>
> if (count > string_length (source))
> {
> for (i = start - 1; i <= (start - 1 + count) && source[i] != '\0'; i++)
> {
> result [i] = source [i];
> }
> result [i + 1] = '\0';
> }
> else if (count < string_length (source) && start >= 1 && start <= (length - count))
> {
> for (i = start - 1; i <= (start - 1 + count); i++)
> {
> result [i] = source [i];
> }
> result [i + 1] = '\0';
> }
No, not corrected, just changed!
There's a lot more wrong with this version than the previous one, but
picking at it like a crow at road kill won't help anyone.
Here's another way to think about this where you won't be side-tracked
by the source length. You are sitting opposite me at a table. I have a
stack of playing cards and I ask you to put the 6 cards starting with
the 3rd onto the table in front of you. You can ask me for the next
card, but that's all. Every time you do I give you the next card or I
hold my hand up and say "null -- there is no next card". How do you
proceed?
--
Ben.
Back to comp.lang.c | Previous | Next — Previous in thread | Find similar | Unroll thread
Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 03:24 -0700
Re: Program to extract a substring from a string; compiling issues Bartc <bc@freeuk.com> - 2015-10-14 11:49 +0100
Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 05:17 -0700
Re: Program to extract a substring from a string; compiling issues Bartc <bc@freeuk.com> - 2015-10-14 13:31 +0100
Re: Program to extract a substring from a string; compiling issues Jorgen Grahn <grahn+nntp@snipabacken.se> - 2015-10-14 13:40 +0000
Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 07:46 -0700
Re: Program to extract a substring from a string; compiling issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 13:42 +0100
Re: Program to extract a substring from a string; compiling issues Alla _ <modelling.data@gmail.com> - 2015-10-14 08:14 -0700
Re: Program to extract a substring from a string; compiling issues Keith Thompson <kst-u@mib.org> - 2015-10-14 11:23 -0700
Re: Program to extract a substring from a string; compiling issues Ben Bacarisse <ben.usenet@bsb.me.uk> - 2015-10-14 21:22 +0100
csiph-web