Breaking a string into substrings

Hello All.
Im writing a program for my c++ class on strings.
I have a program written and im stuck where it asking me to break apart a string that holds a date
for example... string origdate="05/10/1984" or it can be origdate="4/15/1984";
1
2
3
I have written a function that sends 3 reference strings and the original string. I am supposed to break the string apart into 3 different dates. one string holds month, other the day and the other the year...
how do i go about doing that? the original date cannot be changed and it will be keyed in by the user.
Thank you for any help!
should i use size type pos?
Find the first occurrence of '/' in origdate. http://www.cplusplus.com/reference/string/string/find/
The substring before that is the month. http://www.cplusplus.com/reference/string/string/substr/

Then, find the next occurrence of '/' in origdate. etc.
the first digits before the first / i am getting easily its the next digits before the last slash and after the first slash that i am having trouble storing

05/10/1998
im having trouble storing the 10 into a seprate string
You might try something like this (i assume that you'll be able to adapt the code as per your requirements):

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>

int main ()
{
    std::string str = "05/10/1990";
    std::string delim = "/";
    std::string dd, mm, yyyy;
    size_t pos;

    pos = str.find(delim);
    dd = str.substr(pos - 2, 2);
    mm = str.substr(pos + 1, 2);
    pos = str.find(delim, pos + 1);
    yyyy = str.substr(pos + 1);

    std::cout << "Date : " << str << ", dd : " << dd << ", mm : " << mm << ", yyyy : " << yyyy << std::endl;

    return 0;
}
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
#include <iostream>
#include <string>

void split( const std::string& date, std::string& month, std::string& day, std::string& year )
{
    std::size_t pos = date.find('/') ;
    if( pos != std::string::npos )
    {
        month = date.substr( 0, pos ) ;
        ++pos ; // get to character after the '/'

        std::size_t pos2 = date.find( '/', pos ) ;
        if( pos != std::string::npos )
        {
            day = date.substr( pos, pos2-pos ) ;
            year = date.substr(pos2+1) ;
            return ; // fine, we are done
        }
    }

    month = day = year = "" ; // parse error
}

int main()
{
    for( const std::string& date : { "05/10/1984", "4/15/84", "September/5/2014" } )
    {
        std::string month, day, year ;
        split( date, month, day, year ) ;
        std::cout << date << ' ' << month << ' ' << day << ' ' << year << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/74d88e3304c92d37
borges please check ur pm
It can be made simpler with a string stream http://www.cplusplus.com/reference/sstream/istringstream/
and std::getline() http://www.cplusplus.com/reference/string/string/getline/

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
#include <iostream>
#include <string>
#include <sstream>

void split( const std::string& date, std::string& month, std::string& day, std::string& year )
{
    std::istringstream stm(date) ;
    if( std::getline( stm, month, '/' ) && std::getline( stm, day, '/' ) && std::getline( stm, year ) ) ; // ok
    else ; // parse error
}

int main()
{
    for( const std::string& date : { "05/10/1984", "4/15/84", "September/5/2014" } )
    {
        std::string month, day, year ;
        split( date, month, day, year ) ;
        std::cout << date << ' ' << month << ' ' << day << ' ' << year << '\n' ;
    }
}

http://coliru.stacked-crooked.com/a/28b70e6cc8461659
Topic archived. No new replies allowed.