The program determines the day of the week on the specified date in the format DDDD-DD-DD . Her problem is that she does not check whether the number is a D sign.
Below I indicated what I entered and what the program considers the date and what the word is:
2015-09-01 [data] 2015-08-aa [word] 2012/08-26 [word] 2015-08-_26 [word] 2012/aa-26 [word] 2012--8-26 [data??] 2012=08-26 [word] 2014-028-26 [word] 2011-(8-26 [word] 2011-(--26 [word] 2011----26 [word] 2311-(8-26 [word] Everything suits except for one line: 2012--8-26 How can this be fixed using only those libraries that are connected?
#include <stdio.h> // printf, scanf, sscanf #include <stdlib.h> #include <time.h> // time_t, struct tm, time, mktime char buf[101]; // Max length int isDate (const char *buf) { if ( buf[4] == '-' && buf[7] == '-' && buf[10] == '\0' ) { time_t rawtime; struct tm * timeinfo; int year, month, day; if (sscanf(buf, "%u-%u-%u", &year, &month, &day) == 3) { time(&rawtime); timeinfo = localtime(&rawtime); timeinfo->tm_year = year - 1900; timeinfo->tm_mon = month - 1; timeinfo->tm_mday = day; mktime(timeinfo); return timeinfo->tm_wday; } } return -1; } int main (void) { int i = 0; const char * weekday[] = { "Sun", "Mon", "Tue", "Wed", "Thu", "Fri", "Sat"}; while(scanf("%100s", buf) != EOF) // Scanning string until end of string. { if ((i = isDate(buf)) != -1) { printf ("date: %s %s", weekday[i], buf); } else // If not all of above - just word. { printf ("word: %s",buf); } printf("\n"); } // end of while return 0; } // end of main