The function/method validateDate accepts an argument dateStr representing a string value which contains a date in the format “DD-MM-YYYY”.
The function/method validateDate must return 1 if the given date is valid. Else the function must return 0.
Your task is to implement the function validateDate so that the program runs successfully.
IMPORTANT: Do not write the main() function as it is already defined.
Example Input/Output 1:
Input:
26-08-2021
Output:
1
Example Input/Output 2:
Input:
09-15-2010
Output:
0
Explanation:
Invalid month 15.
Example Input/Output 3:
Input:
39-12-2100
Output:
0
Explanation:
Invalid date 39 in December.
Example Input/Output 4:
Input:
29-02-2021
Output:
0
Explanation:
Invalid date 29 in February (2021 is not a leap year).
#include <stdio.h>
#include <stdlib.h>
#include<string.h>
int isLeap(int n)
{
return (((n%4==0)&&((n%400==0)||(n%100!=0))));
}
int validateDate(char *dateStr)
{
char *tok=strtok(dateStr,"-");
int days[13]={0,31,28,31,30,31,30,31,31,30,31,30,31};
int arr[3];
for(int i=0;tok!=NULL;i++)
{
arr[i]=atoi(tok);
tok=strtok(NULL,"-");
}
if(isLeap(arr[2]))
days[2]++;
if((arr[1]>=1&&arr[1]<=12)&&(arr[0]>=1&&arr[0]<=days[arr[1]]))
return 1;
return 0;
}
int main()
{
char dateStr[11];
scanf("%s", dateStr);
printf("%d", validateDate(dateStr));
return 0;
}
Leave a Reply