Advertisement
Aritra15

highestAndLowestDate

Sep 26th, 2022
825
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.08 KB | None | 0 0
  1. #include<stdio.h>
  2. #include<string.h>
  3. int main()
  4. {
  5.     int d1=-1,d2=33,m1=-1,m2=13,y1=-1,y2=20000;
  6.     while(1)
  7.     {
  8.         char s[11],s1[3]="",s2[3]="",s3[5]="";
  9.         int count=0,day,month,year;
  10.  
  11.         printf("Enter a date: ");
  12.         scanf("%s",s);
  13.  
  14.         for(int i=0; s[i]!='\0'; i++)
  15.         {
  16.             if(s[i]=='/')
  17.             {
  18.                 count++;
  19.             }
  20.             if(s[i]!='/' && count==0)
  21.             {
  22.                 //strncat concatenates(joins) a string s1 with a character which is in s[i] and 1 is the size of character
  23.                 strncat(s1,&s[i],1);
  24.             }
  25.             else if(s[i]!='/' && count==1)
  26.             {
  27.                 strncat(s2,&s[i],1);
  28.             }
  29.             else if(s[i]!='/' && count==2)
  30.             {
  31.                 strncat(s3,&s[i],1);
  32.             }
  33.         }
  34.         //atoi(string) converts string to int
  35.         day=atoi(s1);
  36.         month=atoi(s2);
  37.         year=atoi(s3);
  38.  
  39.         if(day==0 && month==0 && year==0)
  40.         {
  41.             printf("Highest date: %d/%d/%d\n",d1,m1,y1);
  42.             printf("Lowest date: %d/%d/%d",d2,m2,y2);
  43.             break;
  44.         }
  45.  
  46.         //checking if the year is highest or not
  47.         if(year>y1)
  48.         {
  49.             y1=year;
  50.             d1=day;
  51.             m1=month;
  52.         }
  53.         else if(y1==year)
  54.         {
  55.             if(month>m1)
  56.             {
  57.                m1=month;
  58.                d1=day;
  59.             }
  60.             else if(month==m1)
  61.             {
  62.                 if(day>d1)
  63.                 {
  64.                     d1=day;
  65.                 }
  66.             }
  67.         }
  68.  
  69.         //check if a year is lowest
  70.         if(year<y2)
  71.         {
  72.             y2=year;
  73.             m2=month;
  74.             d2=day;
  75.         }
  76.         else if(year==y2)
  77.         {
  78.             if(month,m1)
  79.             {
  80.                m2=month;
  81.                d2=day;
  82.             }
  83.             else if(month==m1)
  84.             {
  85.                 if(day<d1)
  86.                 {
  87.                     d1=day;
  88.                 }
  89.             }
  90.         }
  91.     }
  92.  
  93. }
  94.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement