Hi All,
I want to parse a big string
char Big_String[150];
into small strings
char Small_String[30][5] ;
I mean to say that Small_String are separated by spaces in the
Big_String and i want to save them them in five different
Small_Strings. There may be 1,2,3 4 or 5 [ at max] Small_Strings
present in the Big_String.
Please write a compact subroutine for me.
TIA,
Shishir
|
|
0
|
|
|
|
Reply
|
shishir1601 (2)
|
11/16/2007 11:08:15 AM |
|
On Fri, 16 Nov 2007 03:08:15 -0800 (PST), Shishir
<shishir1601@gmail.com> wrote:
> I want to parse a big string
>
> char Big_String[150];
>
> into small strings
>
> char Small_String[30][5] ;
>
>
> I mean to say that Small_String are separated by spaces in the
>Big_String and i want to save them them in five different
>Small_Strings. There may be 1,2,3 4 or 5 [ at max] Small_Strings
>present in the Big_String.
>
>Please write a compact subroutine for me.
I've got a better idea. You write it, then ask if/when you get stuck.
http://www.parashift.com/c++-faq-lite/how-to-post.html#faq-5.2
--
PGP key ID 0xEB7180EC
|
|
0
|
|
|
|
Reply
|
me4 (18696)
|
11/16/2007 11:16:35 AM
|
|
Shishir wrote:
> Hi All,
>
> I want to parse a big string
>
> char Big_String[150];
>
> into small strings
>
> char Small_String[30][5] ;
>
>
> I mean to say that Small_String are separated by spaces in the
> Big_String and i want to save them them in five different
> Small_Strings. There may be 1,2,3 4 or 5 [ at max] Small_Strings
> present in the Big_String.
Then why have you declared Small_String as appropriate for 30 strings of
at most 4 bytes each?
> Please write a compact subroutine for me.
We don't tend to do homework on request...
|
|
0
|
|
|
|
Reply
|
mark_bluemel (848)
|
11/16/2007 11:24:26 AM
|
|
Shishir wrote:
> Hi All,
>
> I want to parse a big string
>
> char Big_String[150];
(Nitpick: that's not automatically a string; it's a char array.
It's not a string unless there's null termination inside it.)
(Stylepick: one can use camelCase; one can use underlined_form;
trying to use them both simultaneously seems to be a computational
marmite ice-cream.)
> into small strings
>
> char Small_String[30][5] ;
>
>
> I mean to say that Small_String are separated by spaces in the
> Big_String and i want to save them them in five different
> Small_Strings. There may be 1,2,3 4 or 5 [ at max] Small_Strings
> present in the Big_String.
So the big string has space-separated small strings in it, OK.
> Please write a compact subroutine for me.
This is not a Do My Work For Me group. But I suggest that you look at
the [specification of the] standard function `strtok`.
--
Chris "def compact() => ();" Dollin
Hewlett-Packard Limited registered no:
registered office: Cain Road, Bracknell, Berks RG12 1HN 690597 England
|
|
0
|
|
|
|
Reply
|
chris.dollin (1683)
|
11/16/2007 11:30:50 AM
|
|
On Nov 16, 1:24 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
> Then why have you declared Small_String as appropriate for 30 strings of
> at most 4 bytes each?
I am sure you ment at most 4 bytes length.
A string whose size is 5 bytes (= char) perfectly fits to a char[5]
|
|
0
|
|
|
|
Reply
|
vippstar (1211)
|
11/16/2007 11:38:44 AM
|
|
<vippstar@gmail.com> schrieb im Newsbeitrag
news:45ee4385-77c1-4425-95de-43ce2a7c565e@w28g2000hsf.googlegroups.com...
> On Nov 16, 1:24 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
>> Then why have you declared Small_String as appropriate for 30 strings of
>> at most 4 bytes each?
> I am sure you ment at most 4 bytes length.
> A string whose size is 5 bytes (= char) perfectly fits to a char[5]
Not if you need to include the terminating nul-byte '\0'
Bye, Jojo
|
|
0
|
|
|
|
Reply
|
nospam.jojo (1344)
|
11/16/2007 11:48:17 AM
|
|
Chris Dollin wrote:
> Shishir wrote:
>
>> I mean to say that Small_String are separated by spaces in the
>> Big_String and i want to save them them in five different
>> Small_Strings. There may be 1,2,3 4 or 5 [ at max] Small_Strings
>> present in the Big_String.
> But I suggest that you look at
> the [specification of the] standard function `strtok`.
>
I was considering an evil solution, writing the original string to a
file, converting space to EOL on output and then reading the file a line
at a time ...
|
|
0
|
|
|
|
Reply
|
mark_bluemel (848)
|
11/16/2007 12:18:48 PM
|
|
On Nov 16, 1:48 pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
wrote:
> <vipps...@gmail.com> schrieb im Newsbeitragnews:45ee4385-77c1-4425-95de-43ce2a7c565e@w28g2000hsf.googlegroups.com...> On Nov 16, 1:24 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
> >> Then why have you declared Small_String as appropriate for 30 strings of
> >> at most 4 bytes each?
> > I am sure you ment at most 4 bytes length.
> > A string whose size is 5 bytes (= char) perfectly fits to a char[5]
>
> Not if you need to include the terminating nul-byte '\0'
>
> Bye, Jojo
sizeof "four" equals 5.
The size of the string literal "four" is 5
strlen("four"); equals 4
The length of the string literal "four" is 4
So, as i said before, a string whose size is 5 bytes perfectly fits to
a char[5]
|
|
0
|
|
|
|
Reply
|
vippstar (1211)
|
11/16/2007 2:10:39 PM
|
|
vippstar@gmail.com wrote:
> On Nov 16, 1:24 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
>> Then why have you declared Small_String as appropriate for 30 strings of
>> at most 4 bytes each?
> I am sure you ment at most 4 bytes length.
> A string whose size is 5 bytes (= char) perfectly fits to a char[5]
This is a bit like arguing about angels on pinheads.
As far as I'm concerned if I talk about a 4-byte string, I'm talking
length of string data not the storage it occupies. The null is a
terminator, and is not, according to my way of thinking part of the
string as such.
|
|
0
|
|
|
|
Reply
|
mark_bluemel (848)
|
11/16/2007 3:01:45 PM
|
|
<vippstar@gmail.com> schrieb im Newsbeitrag
news:08e4daa7-17b6-44b1-9dc3-0ec1f8717d14@y5g2000hsf.googlegroups.com...
> On Nov 16, 1:48 pm, "Joachim Schmitz" <nospam.j...@schmitz-digital.de>
> wrote:
>> <vipps...@gmail.com> schrieb im
>> Newsbeitragnews:45ee4385-77c1-4425-95de-43ce2a7c565e@w28g2000hsf.googlegroups.com...>
>> On Nov 16, 1:24 pm, Mark Bluemel <mark_blue...@pobox.com> wrote:
>> >> Then why have you declared Small_String as appropriate for 30 strings
>> >> of
>> >> at most 4 bytes each?
>> > I am sure you ment at most 4 bytes length.
>> > A string whose size is 5 bytes (= char) perfectly fits to a char[5]
>>
>> Not if you need to include the terminating nul-byte '\0'
>>
>> Bye, Jojo
>
> sizeof "four" equals 5.
> The size of the string literal "four" is 5
> strlen("four"); equals 4
> The length of the string literal "four" is 4
>
> So, as i said before, a string whose size is 5 bytes perfectly fits to
> a char[5]
True, however the OP wahted to cuta an array of 150 chars into strings and
fit them into 30 arrays of 5 chars (actually he wanted the the other way
round, presumably), which won't fit, as every string split adds one
terminating '\0'.
Bye, Jojo
|
|
0
|
|
|
|
Reply
|
nospam.jojo (1344)
|
11/16/2007 3:22:05 PM
|
|
Shishir wrote:
>
> I want to parse a big string
>
> char Big_String[150];
>
> into small strings
>
> char Small_String[30][5] ;
>
> I mean to say that Small_String are separated by spaces in the
> Big_String and i want to save them them in five different
> Small_Strings. There may be 1,2,3 4 or 5 [at max] Small_Strings
> present in the Big_String.
So do it. You might also consider how you process an input of:
"abcdef......enoughtoreach147chars....xyz B".
--
Chuck F (cbfalconer at maineline dot net)
<http://cbfalconer.home.att.net>
Try the download section.
--
Posted via a free Usenet account from http://www.teranews.com
|
|
0
|
|
|
|
Reply
|
cbfalconer (19183)
|
11/16/2007 5:22:51 PM
|
|
|
10 Replies
28 Views
(page loaded in 0.095 seconds)
|