0

I need some tips on an assignment that I am currently working on. I need to create a program for converting time specs (n1y n2n n3d n4h n5m n6s -- note the amount of whitespace can vary between the different inputs) to seconds. One of the functions that I need to create is a function to validate the time spec input (as a const char*). The function header looks as follows:

// is_legal_time_spec:
//      Checks if ctime_spec is a valid time spec
// In:
//      ctime_spec != NULL
// Out:
//      return -- true if the ctime_spec is a valid time spec
//                false if otherwise
bool is_legal_time_spec( const char *ctime_spec ) {
    return false;
}

Ive had some ideas about how to use the strpbrk(3) function for this, but I was wondering if there is another easier function that I can use to check the format of the time spec input.

Thanks in advance for your help.

Vlad G
  • 28
  • 2

1 Answers1

0

Use strtok to break string into words, and then validate each words separately by parsing it or using sscanf. Then do simple mathematics to convert into time specs into seconds

Karthik
  • 129
  • 1
  • 3
  • [Read here](http://stackoverflow.com/questions/4588581/which-functions-in-the-c-standard-library-commonly-encourage-bad-practice) first please, before recommending `strtok()`! – πάντα ῥεῖ Oct 30 '14 at 23:39