Picking apart strings

I am having troubles picking apart a string. I don't know how exactly to take it apart to convert a date. They have to enter a date in this form: 03/05/13 or 3/5/13. The program would have to convert it to the format May 5, 2013. I am just having troubles breaking it up into groups. Help would be appreciated.
Till now I though that May is the fifth month and March is the third month....
You can use strtok to first pick them apart
http://www.cplusplus.com/faq/sequences/strings/split/#strtok

1
2
3
4
5
6
7
8
9
10
11
12
13
#include <stdio.h>
#include <string.h>
int main()
{
  char s[] = "03/05/2013";
  const char* p;

  for (p = strtok( s, "/" );  p;  p = strtok( NULL, "/" ))
  {
    printf( "%s\n", p );
  }
  return 0;
}


You can then deal with each component separately.
For a C++ program you should prefer using a stringstream to parse your std::string.
Well, that could be either May 3, 2013 or March 5, 2013 depending on location.

To get the day, month and year parts, you could use a stringstream:
1
2
3
4
5
6
    string date = "3/5/13";
    istringstream ss(date);
    int dd, mm, yy;
    char dummy;
    ss >> dd >> dummy >> mm >> dummy >> yy;
    cout << dd << " " << mm << " " << yy << endl;
@Kinley Or you can use the C++ programming language:
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
#include <iostream>
#include <string>
#include <sstream>

int main()
{
    std::cout << "Please enter a date MM/DD/YY:" << std::endl;
    std::string m, d, y;
    std::getline(std::cin, m, '/'); //get month
    std::getline(std::cin, d, '/'); //get day
    std::getline(std::cin, y); //get year

    int month, day, year;
    std::istringstream(m) >> month;
    std::istringstream(d) >> day;
    std::istringstream(y) >> year;

    std::cout << "Month: " << month << std::endl;
    std::cout << "Day: " << day << std::endl;
    std::cout << "Year: " << year << std::endl;
}
http://ideone.com/UBYS03

@Chervil I am not entirely sure how you expect that to work.
Last edited on
Did you try it? It looks correct to me.
http://ideone.com/1LaoUV
I guess this works by magic; trying to read the integers with the / after them should put the stream into an error state, shouldn't it? I thought the only delimiters were white space and new lines?
Last edited on
No, because it was able to extract a number before the character. The extraction will stop when it encounters a non-numeric character. It will only error if the first character is not a number.

By the way since the variables are integers a decimal point would also stop the processing. So a string like "3.3.33" should still produce 3 valid integers.

Jim
Last edited on
Could we do some regex stuff with this?
http://cplusplus.com/reference/regex/
The only justification I offer for my code is that it was successfully tested before I posted it. There may be theoretical objections, that's a separate matter.
If you're going with C++11, std::get_time seems more appropriate than std::regex
http://en.cppreference.com/w/cpp/io/manip/get_time
Topic archived. No new replies allowed.