parsing string on a certain character

Discussion in 'C++' started by msullivan3, Mar 30, 2010.

  1. msullivan3

    msullivan3 New Member

    Joined:
    Apr 27, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    Hi,

    i am reading in a date string as hh:mm:ss and trying to store the values in hh, mm, and ss. the input would read as something like 21:43:33. I have looked at some functions like strtok and strchr but not really sure which is most effective. Would appreciate it if someone has an easy answer to this

    Thanks
     
  2. karthigayan

    karthigayan New Member

    Joined:
    Feb 19, 2010
    Messages:
    33
    Likes Received:
    0
    Trophy Points:
    0
    strtok is the best way for this,

    Code:
    #include <stdio.h>
    #include <string.h>
    int main ()
    {
      char str[] ="15:30:12";
      char *hh,*mm,*ss;
      hh = strtok (str,":");
      mm = strtok (NULL,":");
      ss = strtok (NULL,":");
      printf("%s,%s,%s",hh,mm,ss);
      return 0;
    }
    
     
  3. msullivan3

    msullivan3 New Member

    Joined:
    Apr 27, 2009
    Messages:
    10
    Likes Received:
    0
    Trophy Points:
    0
    this will work for me. thanks for your reply!
     
  4. raju00003

    raju00003 New Member

    Joined:
    Dec 22, 2009
    Messages:
    5
    Likes Received:
    0
    Trophy Points:
    0
    here is the Code for what you need ..try this

    Code:
    
    #include <string>
    
    #include <iostream>
    
    using namespace std;
    
    int splitString( string input,string seperatior);
    
    int main()
    {
    
    
        std::string input = "21:30:33";
    
        splitString (input,":");
    
        return 0;
    }
    
    int splitString (string input,string seperator)
    {
    
        size_t i = input.find(seperator);
    
        if ( i!=string::npos )
        {
        
            size_t y = 0;
    
            string hour = "";
    
            if (!input.empty())
            {
                while ( y!=i)
                {
                    hour += input[y++];
                }            
            }
    
            cout << hour << endl;
    
        }
        
        return 0;
    }
    
    
    ;)
     
  5. xpi0t0s

    xpi0t0s Mentor

    Joined:
    Aug 6, 2004
    Messages:
    3,009
    Likes Received:
    203
    Trophy Points:
    63
    Occupation:
    Senior Support Engineer
    Location:
    England
    Rather than calling lots of functions, if the input format is guaranteed you can do something like:
    Code:
    char *tm="21:43:33";
    int hh=tm[0]*10+tm[1]-'0'*11;
    int mm=tm[3]*10+tm[4]-'0'*11;
    int ss=tm[6]*10+tm[7]-'0'*11;
    
    especially if after splitting the strings into smaller strings you're then converting them to numbers.
     

Share This Page

  1. This site uses cookies to help personalise content, tailor your experience and to keep you logged in if you register.
    By continuing to use this site, you are consenting to our use of cookies.
    Dismiss Notice