parsing a big string in smaller strings.

S

Shishir

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
 
K

Keith Willis

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
 
M

Mark Bluemel

Shishir said:
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...
 
C

Chris Dollin

Shishir said:
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`.
 
V

vippstar

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]
 
J

Joachim Schmitz

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
 
M

Mark Bluemel

Chris said:
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 ...
 
V

vippstar

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]
 
M

Mark Bluemel

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.
 
J

Joachim Schmitz

Newsbeitrag
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
 
C

CBFalconer

Shishir said:
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".
 

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
473,744
Messages
2,569,482
Members
44,901
Latest member
Noble71S45

Latest Threads

Top