0

I have to check if the incomming time is exactly in hh:mm format in C programming.

The time will be stored in a string.
If the time is not in hh:mm format (example 22:00 or 10:34 ) the program should show an error.
If the time is in correct format, the program should proceed. Is there any predefined function to check the timing in C?

moffeltje
  • 4,521
  • 4
  • 33
  • 57
Renega
  • 11
  • 1

2 Answers2

0

AFAIK, there is no standard function that does this check. However, it's easy to write your own, since you know that

  • the char at index 0 must be between 0 and 2
  • the char at index 1 can be any digit
  • the char at index 2 must be a colon
  • the char at index 3 must be between 0 and 5
  • the char at index 4 can be any digit
  • the char at index 5 must be a NUL terminator

The only extra rule that you need to enforce is that if the first char is a 2, then the second char must be between 0 and 3.

So the function (which returns 1 if valid, 0 otherwise) looks like this

int validate( char *str )
{
    if ( str[0] < '0' || str[0] > '2' )
        return 0;
    if ( str[1] < '0' || str[1] > '9' )
        return 0;
    if ( str[2] != ':' )
        return 0;
    if ( str[3] < '0' || str[3] > '5' )
        return 0;
    if ( str[4] < '0' || str[4] > '9' )
        return 0;
    if ( str[5] != '\0' )
        return 0;

    if ( str[0] == '2' && str[1] > '3' )
        return 0;

    return 1;
}
user3386109
  • 34,287
  • 7
  • 49
  • 68
  • 1
    Some minor nitpicks: returning 0 almost always in C indicates success and so shouldn't be used as you have. Also, str[5] != '\0' can break a successful parse eg hh:mm:ss and shouldn't be there unless he specifically needs it. And with str[0] what if there's a leading 0 e.g. 0hh:mm:ss? Your attempt is very fragile and would be better left to him to figure out the logic he needs by showing him how to parse a string. – Jason White Jul 20 '15 at 09:02
  • @JasonWhite I submit for your consideration the `isdigit` function. And the OP did say *"exactly in hh:mm format"*, which implies that hh:mm:ss is not valid. – user3386109 Jul 20 '15 at 09:09
  • I suppose I'm just used to writing more robust code then ;P. But re: 0, it's still bad form to use anything but 0 to indicate success with int. You'd be better off using BOOLEAN to indicate you mean to return either 1 or 0, as that way it will be correctly interpreted as 1 indicating success. – Jason White Jul 20 '15 at 09:19
  • 1
    @JasonWhite Except for the fact that in C you can have `return 1` which indicates `true` for any method with return type `bool`. So I guess the people who wrote `stdbool.h` aren't used to writing robust code then :P – M. Shaw Jul 20 '15 at 09:26
  • 1
    Did you even read my comment above yours? Also, BOOLEAN doesn't require an include. – Jason White Jul 20 '15 at 09:49
-2

You could use strtok() to parse the line, and then check that each token is a valid number for a time.

int hour = atoi(strtok( lineToParse, ":" )) ;

int minute = atoi(strtok( NULL, ":" )) ;

//logic testing here
moffeltje
  • 4,521
  • 4
  • 33
  • 57
Jason White
  • 666
  • 4
  • 10
  • 23
  • You should make sure that `strtok` doesn't return `NULL`. Not my downvote, BTW. – Spikatrix Jul 20 '15 at 08:22
  • That's where his logic testing comes in. This is to show him how to parse a string, but it's up to him to figure out the proper testing and error conditions to get it to work to his specifications. – Jason White Jul 20 '15 at 08:28
  • After the call to `atoi` no amount of *"logic testing here"* can be used to validate the string. Your suggestion makes it impossible to detect an invalid string like `"00001hello:0034world"`. Also, a `NULL` return from `strtok` will cause the program to crash, since you aren't checking the return value. – user3386109 Jul 20 '15 at 09:16
  • Thank you Harbison. My answer was meant to point him in the right direction and educate himself further. Not to gather meaningless points on stackoverflow. Teach a man to fish and all that. – Jason White Jul 20 '15 at 09:25
  • To quote your comment on moffeltje's now-deleted answer, *"I downvoted because your function will return certain garbage values as valid."* And yours is worse, because you failed to do even the simplest of error checking on a function that can and does return `NULL`. You didn't teach him to fish, you taught him to drown while attempting to fish. – user3386109 Jul 20 '15 at 09:37
  • If you're using stackflow to copy and paste answers, the problem isn't my answer. He clearly has a very, if not extremely, limited knowledge of C and the standard library. This is to give him some direction on where to start solving his problem and go look up those functions and figure it out, not to give him working code. I'd rather help someone learn to become a better programmer which doing his work for him certainly won't help. That's why I post. – Jason White Jul 20 '15 at 09:48