deanfamily11 wrote:
I have a program that gets a date from the user in the format dd/mm/yy, and
I need to extract the d's, m's, and y's to be used within the program. I
have the program get the date as a string. How can I get the days, month,
and year numbers out of it?
You may use sscanf() for string parsing.
#include <stdio.h>
struct date
{
unsigned short year, month, day;
};
int str_to_date(char const* s, date* d)
{
return 3 == sscanf(s, "%2hu/%2hu/%4hu", &d->day, &d->month,
&d->year)
? 0
: -1
;
}
int main(int ac, char** av)
{
date d;
if(str_to_date(av[1], &d))
printf("bad date format\n");
else
printf("date: %02hu/%02hu/%04hu\n", d.day, d.month, d.year);
}