By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
424,955 Members | 1,768 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 424,955 IT Pros & Developers. It's quick & easy.

parsing a string into substring and integers

P: n/a
Hello all, what is the easiest way to parse the following
string into components?

char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006

Thanks for any help!
Sep 14 '06 #1
Share this Question
Share on Google+
8 Replies


P: n/a
monkeys paw wrote:
Hello all, what is the easiest way to parse the following
string into components?

char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006
If the input format is fixed, use strncpy, passing the start position
and size of each field.

If you want to convert the number to integer, strtol and friends can help.

--
Ian Collins.
Sep 14 '06 #2

P: n/a
Ian Collins wrote:
monkeys paw wrote:
Hello all, what is the easiest way to parse the following
string into components?

char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006
If the input format is fixed, use strncpy, passing the start position
and size of each field.

If you want to convert the number to integer, strtol and friends can help.
Or just let sscanf do all the work for you...

int r = sscanf(s, "%2s%4d%4d", state, &number, &calendar_year);

--
Peter

Sep 14 '06 #3

P: n/a
Ian Collins <ia******@hotmail.comwrites:
monkeys paw wrote:
>Hello all, what is the easiest way to parse the following
string into components?

char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006
If the input format is fixed, use strncpy, passing the start position
and size of each field.
strncpy() won't do what you want (it rarely does). It doesn't always
terminate the target with a '\0' character (i.e., the target won't
necessarily be a string). It copies a '\0' character only if it reads
the '\0' from the source string, and the only '\0' available is the
one at the very end.

If the fields are fixed-size, you can use memcpy() and then set the
'\0' terminator explicitly:

memcpy(state, s, 2);
state[2] = '\0';
memcpy(calendar_year, s+2, 4);
calendar_year[4] = '\0';
memcpy(number, s+6, 5);
calendar_year[5] = '\0';

This code is untested and ugly. In real life, you'd want to use
declared constants rather than "magic numbers".

There are various ways to generalize this code; how you do so depends
on how your problem generalize.
If you want to convert the number to integer, strtol and friends can help.
--
Keith Thompson (The_Other_Keith) ks***@mib.org <http://www.ghoti.net/~kst>
San Diego Supercomputer Center <* <http://users.sdsc.edu/~kst>
We must do something. This is something. Therefore, we must do this.
Sep 15 '06 #4

P: n/a
Peter Nilsson wrote:
Ian Collins wrote:
>monkeys paw wrote:
>>Hello all, what is the easiest way to parse the following
string into components?
>>>
char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006
If the input format is fixed, use strncpy, passing the start position
and size of each field.

If you want to convert the number to integer, strtol and friends can help.

Or just let sscanf do all the work for you...

int r = sscanf(s, "%2s%4d%4d", state, &number, &calendar_year);
Thank you for doing his homework.

--
Regards,
Stan Milam
================================================== ===========
Charter Member of The Society for Mediocre Guitar Playing on
Expensive Instruments, Ltd.
================================================== ===========
Sep 15 '06 #5

P: n/a
Stan Milam wrote:
Peter Nilsson wrote:
>Ian Collins wrote:
>>monkeys paw wrote:
Hello all, what is the easiest way to parse the following
string into components?
>>>>
char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006

If the input format is fixed, use strncpy, passing the start position
and size of each field.

If you want to convert the number to integer, strtol and friends can
help.

Or just let sscanf do all the work for you...

int r = sscanf(s, "%2s%4d%4d", state, &number, &calendar_year);

Thank you for doing his homework.
In this case that is not true (the homework thing). I have been
programming 15 years but haven't really used C in over 10. Just
a bit rusty. Thank you all for the help. I'll shall employ
it thusly at my JOB!
Sep 15 '06 #6

P: n/a
monkeys paw wrote:
<snip>
I'll shall employ it thusly at my JOB!
Just make sure you fix the subtle flaw. ;-)

--
Peter

Sep 15 '06 #7

P: n/a
Peter Nilsson wrote:
monkeys paw wrote:
<snip>
>I'll shall employ it thusly at my JOB!

Just make sure you fix the subtle flaw. ;-)
HAHA!! I did get bit by that, if you mean that number and calendar year
were in the incorrect order :) NICE!!
Sep 15 '06 #8

P: n/a
monkeys paw posted:
char state[3];
int number;
int calendar_year;

int main()
{

char *s = "CA200617456";

}

Firstly, why are you using a "pointer to non-const" to store the address of
read-only data?

What would need to happen after the parse is this:

state= "CA"
number=17456
calendar_year=2006

Well here's a solution which doesn't use the Standard Library:

char const *p = "CA200617456";

state[0] = *p++; state[1] = *p++; state[2] = 0;

calender_year = (*p++ - '0') * 1000;
calender_year += (*p++ - '0') * 100;
calender_year += (*p++ - '0') * 10;
calender_year += (*p++ - '0');

number = <in a similar fashion as above>

--

Frederick Gotham
Sep 15 '06 #9

This discussion thread is closed

Replies have been disabled for this discussion.