Help splitting a simple date string

Y

yogi_bear_79

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

Daniel T.

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<string> splitDate( const string& str ) {
vector<string> result;
string::size_type i = str.find( ' ' );
result.push_back( str.substr( 0, i ) );
string::size_type last = i;
i = str.find( ' ', i + 1 );
result.push_back( str.substr( last, i - last ) );
result.push_back( 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.
 
Y

yogi_bear_79

If you know the format is exactly as you show above, then it is a
simple matter of finding the spaces.

   vector<string> splitDate( const string& str ) {
      vector<string> result;
      string::size_type i = str.find( ' ' );
      result.push_back( str.substr( 0, i ) );
      string::size_type last = i;
      i = str.find( ' ', i + 1 );
      result.push_back( str.substr( last, i - last ) );
      result.push_back( 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(longDate,100);

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

Alf P. Steinbach

* yogi_bear_79:
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!

Many ways. One has already been illustrated by Daniel T else-thread,
using built-in std::string functions. The simplest would possibly be to
use a regular expression, but then you need to know about those.

Somewhere in the middle (disclaimer: code not touched by compiler's hands):

std::vector<std::string> splitOnWhitespace( std::string const& s )
{
std::istringstream stream( s );
std::string component;
std::vector<std::string> result;

while( s >> component ) { result.push_back( component ); }
return result;
}

Here I just let delimiters such as that comma hang on. They'll be
removed by conversion to numeric later.

Cheers, & hth.,

- Alf
 
J

Jeff Schwab

yogi_bear_79 said:
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!

#include <cassert>
#include <iterator>
#include <sstream>
#include <string>
#include <vector>

int main() {

std::istringstream stream("February 27, 2008");
std::istream_iterator<std::string> in(stream), end;
std::vector<std::string> strings(in, end);

assert(strings.size() == 3);
assert(strings.at(0) == "February");
assert(strings.at(1) == "27,");
assert(strings.at(2) == "2008");
}
 
M

mdatye

Hello,

Small suggestion; IMHO, instead of falling back to char* and strtok it
would be beneficial in the longer run to work with std::string and
"more advanced" topics. If you are doing this for a school assignment
where you have freedom and a couple of hours to spare, have a look at
boost like libraries which would do interesting stuff for you.

You already have most of the solutions listed anyways.

Cheers,
~M
 
D

Daniel T.

If you know the format is exactly as you show above, then it is a
simple matter of finding the spaces.
   vector<string> splitDate( const string& str ) {
      vector<string> result;
      string::size_type i = str.find( ' ' );
      result.push_back( str.substr( 0, i ) );
      string::size_type last = i;
      i = str.find( ' ', i + 1 );
      result.push_back( str.substr( last, i - last ) );
      result.push_back( 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(longDate,100);

        char *month = strtok_s(longDate, " ", &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...
 

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,769
Messages
2,569,582
Members
45,070
Latest member
BiogenixGummies

Latest Threads

Top