0

iam working on program which separates characters by meaning. Currently i got issue with function konverzia. It should add name of day (wed, thu, fri...) to date string, but it doesnt give a shit. Date have to look like this Wed 2012-02-01 Thanks for any ideas.

#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <time.h>

 void vstup (int i, char buffer[], int s, int c)
  {
        printf("\n Type text for analysis: \n ");    
        scanf ("%80s", buffer);      /* Read a string into buffer  */
  }
 void detekcia (int i, char buffer[], int s, int c, int z)
  {
         while(buffer[i] != '\0') {
            if(isalpha(buffer[i]))
                 s++;                /* Increment letter count     */
            if(isdigit(buffer[i++]))
                 c++;                /* Increment digit count      */
            if(ispunct(buffer[i++]))
                 z++;
                                  }
        if (s >= 1 && c >= 1)
          printf("\n word: %s \n", buffer);     /*kombinacia cisla a pismena*/
        if (s >= 1 && c == 0)
          printf("\n word: %s \n", buffer);     /*WORD*/
        if (s == 0 && c >= 1 && z == 0)
          prvocislo (i,buffer,s,c);             /*NUMBER*/
        if (s >= 0 && c >= 1 && z >= 1)
          datum (i,buffer,s,c,z);               /*DATE*/
 }
  int prvocislo (int i, char buffer[], int s, int c) /*prime number fun*/
  {
        int x,count=0;
        int k = atoi(buffer);

        for (x=2; x<=k/2;x++){
            if (k%x==0){
                count++;
                   break;
                }
        }
        if (count==0 && k!=1 && k!=0)
            printf("\n number: %d (prime)\n",k);
        else
            printf("\n number: %d",k);
  }
  int datum (int i, char buffer[], int s, int c, int z)
  {
        int q = 0;
        char d[2];
        char m[2];
        char r[4];

        if (buffer[4]=='-' && buffer[7]=='-')
           q++;                                                                                           
    if (q==1 && buffer[5]<='1')
           q++;
        if (q==2 && buffer[8]<='3')
           q++;
        if (q==3){
           q++;
           d[1]==buffer[9];
           d[0]==buffer[8];
           m[1]==buffer[6];
           m[0]==buffer[5];
           r[3]==buffer[3];
           r[2]==buffer[2];
           r[1]==buffer[1];
           r[0]==buffer[0];
                 }
        if (q==4)
           konverzia (d,m,r);
   }
   int konverzia (char d[], char m[], char r[])
  {
        int rr = atoi(r);
        int dd = atoi(d);
        int mm = atoi(m);
        int ret;
        struct tm info;
        char date[10];

        info.tm_year = rr - 1900;
        info.tm_mon = mm - 1;
        info.tm_mday = dd;
        info.tm_wday = dd;

        ret = mktime(&info);
        if (ret == -1){
        printf("time error");
                      }
        else{
           strftime(date, sizeof(date),"%c",&info);
           printf(date);
            }

        return(0);
  }
  int main() {

  char buffer[80];
  int i = 0;
  int s = 0; /*pismeno*/
  int c = 0; /*cislo*/
  int z = 0;
  vstup (i,buffer,s,c);
  detekcia (i,buffer,s,c,z);
  return 0;
  }
2
  • "info.tm_wday" gives you 0-6 for Mon, Thu, ... - so go to google and look for "switch/case" and solve it.
    – EGOrecords
    Commented Oct 16, 2015 at 7:09
  • 1
    Ten chars is not even enough to print a regular data like 2015-10-16, let alone one with a weekday. If you want to enforce a certain date format, you can construct it on your own instead of relying on the preferred format %c.
    – M Oehm
    Commented Oct 16, 2015 at 7:13

1 Answer 1

1

If you want a particular format, you have to constructa custom format string. The possible entries are listed in the documentation for strftime. The %c format that you use is just the preferred format of your locale.

There are some issues with your code:

  • You shouldn't set tm_wday to the day of the month. tm_wday is an index of the weekday, starting with 0 for Sunday.
  • You should initialise your struct tm to zero, so that fields that you don't assign don't have nonsense values.
  • Your date buffer is too short. strftime guards against overflow, but the date will be truncated.
  • Don't printf strings directly, use printf("%s", str) instead. In particular, the format string shouldn't be a string you don't control. For example, an unknown % sequence in strftime might not get converted and will falsely be interpreted as printf format without the correct argument. Some static analysis tools insist on using only string literals as format strings. Good practice, I think.

So:

int konverzia(const char d[], const char m[], const char r[])
{
    int rr = atoi(r);
    int dd = atoi(d);
    int mm = atoi(m);

    struct tm info = {0};

    info.tm_year = rr - 1900;
    info.tm_mon = mm - 1;
    info.tm_mday = dd;

    if (mktime(&info) == -1) {
        printf("time error\n");
        return -1;
    } else {
        char date[20];

        strftime(date, sizeof(date), "%a %Y-%m-%d", &info);
        printf("%s\n", date);
    }

    return 0;
}
3
  • Thank you very much for your time, i really appreciate your help, but look what it did to output:::::::..... Type text for analysis: 2001-11-11 Tue -1-11-30:................... and it doesnt matter which input i type, it is always giving this Tue date
    – MrBr
    Commented Oct 16, 2015 at 8:06
  • I haven't looked much at your program outside the konverzia function and I have tested it with a few dates explicitly, like konverzia("16", "10", "2015"); I suggest you check the strings that you pass in. Doing a few more tests, I see that mktime "normalises" bad input, so that 31 June is really 1 July. That means that you should add extra checks for validity instead of relying on mktime returning -1.
    – M Oehm
    Commented Oct 16, 2015 at 8:20
  • Thanks a lot! finally i found the mistake, and now it works great :)
    – MrBr
    Commented Oct 19, 2015 at 12:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.