parsing a string into substring and integers

M

monkeys paw

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!
 
I

Ian Collins

monkeys said:
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.
 
P

Peter Nilsson

Ian said:
monkeys said:
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);
 
K

Keith Thompson

Ian Collins said:
monkeys said:
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.
 
S

Stan Milam

Peter said:
Ian said:
monkeys said:
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.
=============================================================
 
M

monkeys paw

Stan said:
Peter said:
Ian said:
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!
 
M

monkeys paw

Peter said:
monkeys paw wrote:


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!!
 
F

Frederick Gotham

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>
 

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

No members online now.

Forum statistics

Threads
473,744
Messages
2,569,484
Members
44,903
Latest member
orderPeak8CBDGummies

Latest Threads

Top