Check The validity Of Date

harshchandra 0 Tallied Votes 178 Views Share

It's a simple program .Just enter any date in dd/mm/yyyy format.If you enter wrong you will get the message .

# include<stdio.h>
# include<conio.h>
# include<string.h>

# include<math.h>
int check_date(char[]);
int converttointeger(char[]);
void main()
{   clrscr();
    char date[11];
    int c;
    puts("ENTER ANY DATE : ");
    gets(date);
    c= check_date(date);
    if(c==0)
    puts("\nINVALID DATE");
    else
    puts("VALID DATE");
    getch();
}

int check_date(char a[])
{  int l,d,y,logic = 0;char dd[4],mm[4],yy[5];
   l = strlen(a);
   if(l!=10)
   return 0;

   else if(a[2] !='/' && a[5] !='/')
   return 0;

   dd[0]=a[0];dd[1]=a[1];dd[2]='\0';
   mm[0]=a[3];mm[1]=a[4];mm[2]='\0';
   yy[0]=a[6];yy[1]=a[7];yy[2]=a[8];
   yy[3]=a[9];yy[4]='\0';
   d = converttointeger(dd);
   y = converttointeger(yy);



   // Case of months having 31 days

   if(strcmp(mm,"01")==0 || strcmp(mm,"03")==0 || strcmp(mm,"05")==0 || strcmp(mm,"07")==0 || strcmp(mm,"08")==0 || strcmp(mm,"10")==0 || strcmp(mm,"12")==0 )
   {  if( d>=1 && d<=31)
      { logic = 1;
      }
   }
   // Case of Februaury
   else if(strcmp(mm,"02")==0)
   {   if(d>=1 && d<=28)
       { logic =1;
       }
       if(d == 29 && y % 4 == 0)
       { logic=1;
       }
   }
   // Case of months having 30 days
   else if(strcmp(mm,"04")==0 || strcmp(mm,"06")==0 || strcmp(mm,"09")==0 || strcmp(mm,"11")==0)
   {
       if(d>=1 && d<=30)
       {  logic =1;
       }
   }

   return logic;
}

int converttointeger(char str[])
{  int r,p,i,k;
   r=0;
   k=0;
   p = pow(10,(strlen(str)-1));
   for(i=0;i<strlen(str);i++)
   {  k= str[i] - '0';
      r += (k*p);
      p/=10;
   }
   return r;
}