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 
  • "conclusion with verification" o_O - Qwertiy ♦
  • @Qwertiy changed the name, I hope will suit - Danya

3 answers 3

In general, the @Discord answer will work, but it can be easier. Change %u to %d and add a check on the positivity of the values ​​obtained. Well, I also wanted to write about strlen .

 if (sscanf(buf, "%d-%d-%d", &year, &month, &day) == 3 && year>0 && month>0 && day>0) 

The problem is that -8 perceived as a number (as opposed to $8 , for example). Actually, then the check for delimiters can probably be removed.

  • Do not tell me how to properly use strlen in this code? - Danya
  • if (strlen(buf) == 11) probably instead of buf[10] == 0 - andy.37
  • Why strlen(buf) == 11 ? In fact, 10. But a direct check buf[10] better, since faster (and even safer). - avp
  • @avp, again, how isDate("") call work? In this code, it is not, but this means that the function is aware of the surrounding code. 10 I may use c-strings too rarely. - andy.37
  • @avp So still change the length check to strlen or leave it as it is? - Danya
 "%u-%u-%u" 
 "%u%*1[-]%u%*1[-]%u" 

However, this is only a format - the numbers themselves are not checked.

  • I tried your format, it did not help. - Danya
  • @Danya, codepad.org/Bh6d9DXG - the month is -8 . This is as correct as 99, that is, the format is not solved. - Qwertiy ♦
  • @Qwertiy, there is an unsigned format, so what month it will turn out is not quite clear, maybe not -8. - andy.37
  • @ andy.37, scanf treats %d and %u same way. - Qwertiy ♦
  • @Qwertiy not sure: d is for a signed argument, and u for an unsigned

http://ideone.com/rWfEMI

 #include <stdio.h> #include <ctype.h> int ishyphen(int ch) { return ch == '-'; } int isdate(const char *s) { unsigned q; int (*f[])(int)= { isdigit, isdigit, isdigit, isdigit, ishyphen, isdigit, isdigit, ishyphen, isdigit, isdigit }; for (q=0; q < sizeof f / sizeof *f; ++q) if (!f[q](s[q])) return 0; return !s[q]; } int main(void) { char s[256]; while(gets(s)) printf("%s - %s\n", s, isdate(s) ? "yes" : "no"); return 0; } 
  • Thanks, but in your case <ctype.h> is used, and you need an option only with those three libraries - Danya
  • @Danya, int isdigit(int ch) { return ch>='0' && ch<='9'; } int isdigit(int ch) { return ch>='0' && ch<='9'; } - and minus the header file. By the way, he is not a library. - Qwertiy ♦
  • @Qwertiy, and this, although it will work, I think, always, but in my opinion, the standard is not guaranteed that the codes of numbers go in a row. - andy.37
  • @ andy.37, and in what encoding do they not go in a row? - Qwertiy ♦
  • @Qwertiy, yes they all go, clearly. But tomorrow I will come up with an encoding that does not go, and? PS, it is interesting to communicate with you, thank you. - andy.37