Click to See Complete Forum and Search --> : Parsing an array


neelmukherjee
November 7th, 2001, 02:06 PM
Hey people,
Could someone tell me a quick method for getting integer values from an array (The array is delimited by / )and put these values in an variable.
So if my array looks like this 10/12/2001 - I would like to put 10 in variable M, 12 in variable D and 2001 in variable Y.
thanks in advance

James Curran
November 7th, 2001, 02:34 PM
First of all, most people would call that a "string", not an "array". (granted, it is also an array, but more people will know what you mean).

The simplest thing to do is use atoi(). It will stop at the first non-digit, and it's doesn't care if the are characters before the char* you give it.
char date[] ='10/12/2001';
int M = atoi(date);
char* ptr = strchr(date,'/') + 1; // ptr points to char passed first '/'
int D = atoi(ptr);
char* ptr = strchr(ptr, '/') +1) // ptr points to char passed second '/'
int Y = atoi(ptr);




Truth,
James
http://www.NJTheater.com
http://www.NovelTheory.com
I don't do it for the points (OK, maybe I do), but rating a post is a good way for me to know if I helped.