Ask your own question, for FREE!
Computer Science 16 Online
OpenStudy (anonymous):

How do you check if a string is a valid date in C.

OpenStudy (e.mccormick):

You would need to parse it and test the parts. Might also be able to assign it to a time structure and see if the assignments work.

OpenStudy (anonymous):

so if I have like 11/12/13. I could split it between the "/" with strtok but how would I check if is going to be a date initially?

OpenStudy (anonymous):

Would I need to go through character by character for (counter = 0; counter == MAX; counter++) { wordArray[counter] = '\0'; } and then check if the first two characters are ints. make sure the next character is not an int, and then repeat that?

OpenStudy (anonymous):

does that sound right @e.mccormick ? I'm new to how parsing works.

OpenStudy (anonymous):

Basically I'm just trying to write an isDate(char* string); function where I pass in random strings and it returns 1 or 0 depending on if the string is a valid date.

OpenStudy (e.mccormick):

Use sscanf to parse it and then you can evaluate it: http://stackoverflow.com/questions/9537072/properly-using-sscanf Or yes, you could evaluate character by character.

OpenStudy (anonymous):

Wouldn't I need to know the length of the string to use sscanf?

OpenStudy (e.mccormick):

No, it does not take the length as an argument.

OpenStudy (e.mccormick):

Set up ints, then use sscanf with the string, the format of the string, and pointers to assign the values to the ints. Then check to see if you got valid results. You might also wrap it all in an exception handler so that if people feed a bad date format it just returns false.

OpenStudy (anonymous):

but I don't know the length of the string so I don't know how many ints to use. do I need to perform some check on the string to see if it is the valid length first?

OpenStudy (e.mccormick):

You do not need to know the length. What you can do is pass off any excess length to another string. If you want to do a sanity check before hand you could get the length just to make sure any excess would fit in your buffer string.

OpenStudy (e.mccormick):

http://en.cppreference.com/w/c/io/fscanf

OpenStudy (e.mccormick):

Ah, and here is a nice, short example: http://www.tutorialspoint.com/c_standard_library/c_function_sscanf.htm

OpenStudy (anonymous):

Hmm, doesn't really makes sense to me. :/

OpenStudy (anonymous):

I don't understand how you use sscanf without knowing the length.

OpenStudy (e.mccormick):

Why would you need the length? Anything outside the numbers could be assigned to something else via %s.

OpenStudy (anonymous):

I don't know, I guess I'll just try to look through it character by character and do it that way. Parsing in C doesn't makes sense to me.

OpenStudy (anonymous):

Thanks for the help though.

OpenStudy (e.mccormick):

At the bottom this it shows how to make a new string of people length: http://www.cs.swarthmore.edu/~newhall/unixhelp/C_strings.html If you make a string of at least the same size as what is given, then any excess can be parsed off into that. It assumes the date comes first.

OpenStudy (anonymous):

This is the input. I break it up into words and then pass each word in. Hello, I am emailing you today in regards to the job posting you sent out on 11/12/13. I am interested in setting up a time to meet in the near future, are you available on 11-20-13 or 12/1/13? Please respond by 11/32/13, Thank you.

OpenStudy (e.mccormick):

Ah, yes, that is a different issue.

OpenStudy (anonymous):

I don't have to check for leap years or anything like that though. Just if the month is 1-12 and the day is 1-31. I think I need to use the ctype.h library

Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!
Can't find your answer? Make a FREE account and ask your own questions, OR help others and earn volunteer hours!

Join our real-time social learning platform and learn together with your friends!