Help splitting a simple date string

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • yogi_bear_79

    Help splitting a simple date string

    I have a simple string (i.e. February 27, 2008) that I need to split
    into three parts. The month, day, and year. Splitting into a string
    array would work, and I could convert day and years to integers
    later. I've bene looking around, and everything I see seems more
    complicated than it should be! Help!
  • Daniel T.

    #2
    Re: Help splitting a simple date string

    On Mar 3, 8:49 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
    I have a simple string (i.e. February 27, 2008) that I need to split
    into three parts. The month, day, and year.  Splitting into a string
    array would work, and I could convert day and years to integers
    later.  I've bene looking around, and everything I see seems more
    complicated than it should be! Help!
    If you know the format is exactly as you show above, then it is a
    simple matter of finding the spaces.

    vector<stringsp litDate( const string& str ) {
    vector<stringre sult;
    string::size_ty pe i = str.find( ' ' );
    result.push_bac k( str.substr( 0, i ) );
    string::size_ty pe last = i;
    i = str.find( ' ', i + 1 );
    result.push_bac k( str.substr( last, i - last ) );
    result.push_bac k( str.substr( i ) );
    return result;
    }

    If you turn the above into your teacher though, he should grade you
    very poorly. You really should have some error checking and probably
    some sort of verification code.

    But the above should at least get you started.

    Comment

    • yogi_bear_79

      #3
      Re: Help splitting a simple date string

      On Mar 3, 9:08 pm, "Daniel T." <danie...@earth link.netwrote:
      On Mar 3, 8:49 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
      >
      I have a simple string (i.e. February 27, 2008) that I need to split
      into three parts. The month, day, and year.  Splitting into a string
      array would work, and I could convert day and years to integers
      later.  I've bene looking around, and everything I see seems more
      complicated than it should be! Help!
      >
      If you know the format is exactly as you show above, then it is a
      simple matter of finding the spaces.
      >
         vector<stringsp litDate( const string& str ) {
            vector<stringre sult;
            string::size_ty pe i = str.find( ' ' );
            result.push_bac k( str.substr( 0, i ) );
            string::size_ty pe last = i;
            i = str.find( ' ', i + 1 );
            result.push_bac k( str.substr( last, i - last ) );
            result.push_bac k( str.substr( i ) );
            return result;
         }
      >
      If you turn the above into your teacher though, he should grade you
      very poorly. You really should have some error checking and probably
      some sort of verification code.
      >
      But the above should at least get you started.

      Thank you so much for the idea. I do believe that it is slightly more
      advanced than I shoul dbe at this point. I've settled on the code
      below, just as soon as I parrse out the comma!

      char longDate[100];
      char* nextToken;

      cout << " \n Enter a date in the following format(February 27,
      2008):";
      cin.getline(lon gDate,100);

      char *month = strtok_s(longDa te, " ", &nextToken);
      char *day = strtok_s(NULL, ",", &nextToken);
      char *year = strtok_s(NULL, "\0", &nextToken);

      Comment

      • Daniel T.

        #4
        Re: Help splitting a simple date string

        On Mar 3, 9:53 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
        On Mar 3, 9:08 pm, "Daniel T." <danie...@earth link.netwrote:
        >
        >
        >
        >
        >
        On Mar 3, 8:49 pm, yogi_bear_79 <yogi_bear...@y ahoo.comwrote:
        >
        I have a simple string (i.e. February 27, 2008) that I need to split
        into three parts. The month, day, and year.  Splitting into a string
        array would work, and I could convert day and years to integers
        later.  I've bene looking around, and everything I see seems more
        complicated than it should be! Help!
        >
        If you know the format is exactly as you show above, then it is a
        simple matter of finding the spaces.
        >
           vector<stringsp litDate( const string& str ) {
              vector<stringre sult;
              string::size_ty pe i = str.find( ' ' );
              result.push_bac k( str.substr( 0, i ) );
              string::size_ty pe last = i;
              i = str.find( ' ', i + 1 );
              result.push_bac k( str.substr( last, i - last ) );
              result.push_bac k( str.substr( i ) );
              return result;
           }
        >
        If you turn the above into your teacher though, he should grade you
        very poorly. You really should have some error checking and probably
        some sort of verification code.
        >
        But the above should at least get you started.
        >
        Thank you so much for the idea. I do believe that it is slightly more
        advanced than I shoul dbe at this point. I've settled on the code
        below, just as soon as I parrse out the comma!
        >
                char longDate[100];
                char* nextToken;
        >
                cout << " \n Enter a date in the following format(February 27,
        2008):";
                cin.getline(lon gDate,100);
        >
                char *month = strtok_s(longDa te, " ", &nextToken);
                char *day = strtok_s(NULL, ",", &nextToken);
                char *year = strtok_s(NULL, "\0", &nextToken);- Hide quoted text -
        >
        }
        The code you show above is considered much more advanced than what I
        showed...

        Comment

        • David Harmon

          #5
          Re: Help splitting a simple date string

          On Mon, 3 Mar 2008 17:49:40 -0800 (PST) in comp.lang.c++,
          yogi_bear_79 <yogi_bear_79@y ahoo.comwrote,
          >I have a simple string (i.e. February 27, 2008) that I need to split
          >into three parts. The month, day, and year.
          My version:

          Comment

          Working...