#include <stdio.h> #include <stdlib.h> #include <ctype.h> typedef struct { int m
ID: 1921415 • Letter: #
Question
#include <stdio.h>#include <stdlib.h>
#include <ctype.h>
typedef struct
{
int month,
day,
year;
} Date;
Date String_To_MDY(char *);
int Validate_Date(Date);
char * Strchcpy(char *T, char *S, int ch); // from S to T stops at ch
// returns * to position after ch
main()
{
char date_string [9];
Date mdy_date;
char response[8];
printf(" Do you want to convert and validate a date?(Y/N): ");
gets(response);
while (toupper(*response) == 'Y')
{
printf(" Enter the date in mm/dd/yy form: ");
gets(date_string);
mdy_date = String_To_MDY (date_string);
printf(" The converted date is the following:");
printf(" Month: %3d", mdy_date.month);
printf(" Day: %3d", mdy_date.day);
printf(" Year: %3d ", mdy_date.year);
if ( Validate_Date(mdy_date) )
printf(" The date is valid.");
else
printf(" The date is invalid.");
printf(" Do you want to convert and validate a date?(Y/N): ");
gets(response);
}
} /* End of main() */
Date String_To_MDY(char * date_ptr)
{
Date mdy_date;
char month[3],
day[3],
year[3];
char * ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchcpy(month, ch_ptr, '/');
++ch_ptr;
ch_ptr = Strchcpy(day, ch_ptr, '/');
++ch_ptr;
Strchcpy(year, ch_ptr, '');
mdy_date.month = atoi(month);
mdy_date.day = atoi(day);
mdy_date.year = atoi(year);
return mdy_date;
} /* End of String_To_MDY() */
char * Strchcpy(char * target, char * source, int ch)
{
while (*source != ch && *source != '')
{
*target = *source;
++target;
++source;
}
*target = '';
return source;
} /* End of Strchcpy() */
int Validate_Date(Date date)
{
int ny_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (date.month < 1 || date.month > 12)
return 0;
if (date.day < 1)
return 0;
if (date.year % 4 == 0)
{
if (date.day > ly_days[date.month])
return 0;
}
else
if (date.day > ny_days[date.month])
return 0;
if (date.year < 80)
return 0;
return 1;
} /* End of Validate_Date() */
Explanation / Answer
// plz see below red coloured comments.
#include <stdio.h>
#include <stdlib.h>
#include <ctype.h>
typedef struct
{
int month,
day,
year;
} Date;
Date String_To_MDY(char *);
int Validate_Date(Date);
char * Strchcpy(char *T, char *S, int ch); // from S to T stops at ch
// returns * to position after ch
main()
{
char date_string [9];
Date mdy_date;
char response[8];
printf(" Do you want to convert and validate a date?(Y/N): ");
gets(response);
while (toupper(*response) == 'Y')
{
printf(" Enter the date in mm/dd/yy form: ");
gets(date_string);
mdy_date = String_To_MDY (date_string);
printf(" The converted date is the following:");
printf(" Month: %3d", mdy_date.month);
printf(" Day: %3d", mdy_date.day);
printf(" Year: %3d ", mdy_date.year);
if ( Validate_Date(mdy_date) )
printf(" The date is valid.");
else
printf(" The date is invalid.");
printf(" Do you want to convert and validate a date?(Y/N): ");
gets(response);
}
} /* End of main() */
Date String_To_MDY(char * date_ptr)
{
Date mdy_date;
char month[3],
day[3],
year[3];
char * ch_ptr;
ch_ptr = date_ptr;
ch_ptr = Strchcpy(month, ch_ptr, '/');
++ch_ptr;
ch_ptr = Strchcpy(day, ch_ptr, '/');
++ch_ptr;
Strchcpy(year, ch_ptr, '');
mdy_date.month = atoi(month);
mdy_date.day = atoi(day);
mdy_date.year = atoi(year);
return mdy_date;
} /* End of String_To_MDY() */
char * Strchcpy(char * target, char * source, int ch)
{
while (*source != ch && *source != '')
{
*target = *source;
++target;
++source;
}
*target = '';
return source;
} /* End of Strchcpy() */
int Validate_Date(Date date)
{
int ny_days [13] = {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
int ly_days [13] = {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31};
if (date.month < 1 || date.month > 12)
return 0;
if (date.day < 1)
return 0;
if (date.year % 4 == 0)
{
if (date.day > ly_days[date.month])
return 0;
}
else
if (date.day > ny_days[date.month])
return 0;
// dude your doing mistake here that y it is coming as invalid. now it will work. run this code now
// if (date.year < 80)
// return 0;
return 1;
} /* End of Validate_Date() */
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.