I need to check whether a date is valid in C, and for this I will have a function called verificarData
that will be passed to it by the user input and will check whether or not it is a valid date.
But I'm in doubt, because I'll have to break this data
to see if it contains:
If it contains two bars between the date
If the day is between 1 and 31.
If the month is between 1 and 12.
Year is in the 1900 to 2100 range
If month is 04, 06, 09 or 11, day can be at most 30;
If month is 02, day can be at most 28; If leap year and month is 02, day can be at most 29.
Which is better to use in case? Use vetor
or even char
and try to break the date and do the checks, and if I can use with char, how to break it in pieces to check?
For example, if the user enters the following value:
Input : 20/03/2009
How can I break so I can stay, 20/
, 03/
and 2009
separated so I can check?
I also need to check if the day, month and year are numeric, but since I am putting bars with numbers I can not use the isdigit
function to check.
Example:
if(isdigit(data)) {
printf("São numéricos.");
}
else {
printf("Não são numéricos.");
}
But it returns as the numbers are not even the user putting the day, month and year with numbers, because because of the bars, it returns as if it were a string. How can I check and return by saying whether or not they are numbers correctly?