CodeGuru Home VC++ / MFC / C++ .NET / C# Visual Basic VB Forums Developer.com
Results 1 to 2 of 2
  1. #1
    Join Date
    Nov 2001
    Posts
    46

    Parsing an array

    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


  2. #2
    Join Date
    Sep 1999
    Location
    NJ
    Posts
    1,299

    Re: Parsing an array

    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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  





Click Here to Expand Forum to Full Width

Featured