This function involves extracting the day, month, year from a processed string.
my code:
int main(void)
{
int date = atoi("161005");
int day = date/10000;
int month = (date/100) % 100;
int year = (date % 100);
printf("%02d/%02d/%02d",day,month,year);
return 0;
}
it works perfectly when the values are in the form dd:mm:yy
however the breaks down when you give it inputs such as 1/1/5 or 1/11/5 or 11/1/5
for 1/1/5 it return 00/1/05
for 1/11/5 it return 01/11/05
for 11/1/5 it return 00/11/15
I was just wondering is there a faster and more efficent way of designing it, instead of using switch statements.
Thanks
my code:
int main(void)
{
int date = atoi("161005");
int day = date/10000;
int month = (date/100) % 100;
int year = (date % 100);
printf("%02d/%02d/%02d",day,month,year);
return 0;
}
it works perfectly when the values are in the form dd:mm:yy
however the breaks down when you give it inputs such as 1/1/5 or 1/11/5 or 11/1/5
for 1/1/5 it return 00/1/05
for 1/11/5 it return 01/11/05
for 11/1/5 it return 00/11/15
I was just wondering is there a faster and more efficent way of designing it, instead of using switch statements.
Thanks