Check if a date is valid or not in C?

1

I need to check whether a date is valid in C, and for this I will have a function called verificarData that will be passed to it by the user input and will check whether or not it is a valid date.

But I'm in doubt, because I'll have to break this data to see if it contains:

  
  • If it contains two bars between the date

  •   
  • If the day is between 1 and 31.

  •   
  • If the month is between 1 and 12.

  •   
  • Year is in the 1900 to 2100 range

  •   
  • If month is 04, 06, 09 or 11, day can be at most 30;

  •   
  • If month is 02, day can be at most 28;
  •   
  • If leap year and month is 02, day can be at most 29.
  •   

    Which is better to use in case? Use vetor or even char and try to break the date and do the checks, and if I can use with char, how to break it in pieces to check?

    For example, if the user enters the following value:

    Input : 20/03/2009
    

    How can I break so I can stay, 20/ , 03/ and 2009 separated so I can check?

    I also need to check if the day, month and year are numeric, but since I am putting bars with numbers I can not use the isdigit function to check.

    Example:

    if(isdigit(data)) {
    printf("São numéricos.");
    }
    else {
    printf("Não são numéricos.");
    }
    

    But it returns as the numbers are not even the user putting the day, month and year with numbers, because because of the bars, it returns as if it were a string. How can I check and return by saying whether or not they are numbers correctly?

        
    asked by anonymous 16.06.2017 / 23:16

    2 answers

    1
      

    For example, if the user enters the following value:

         

    Input : 20/03/2009

         

    How can I break so I can stay, 20/ , 03/ and 2009 separated so I can check?

    Roles

    Use the functions below.

    • strtok - breaks a string in tokens, given a specific delimiter (use / ).

    • strtol - converts a string to a long integer.

    • strstr - finds the first occurrence of a specific substring in a string (use // ).

    • isdigit - checks if the character passed as an argument is a digit .

    Logic

    Base the code below. Also see working online here .

    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int verificarNumero(char *entrada) {
      int i;
    
      for (i = 0; entrada[i] != '
    #include <stdio.h>
    #include <stdlib.h>
    #include <string.h>
    
    int verificarNumero(char *entrada) {
      int i;
    
      for (i = 0; entrada[i] != '%pre%'; i++)
      {
        if (entrada[i] != '/' && !isdigit(entrada[i]))
        {
          return 0;
        }
      }
    
      return 1;
    }
    
    int verificarData(char *entrada)
    {
      const char substring[3] = "//";
    
      if (strstr(entrada, substring) != NULL)
      {
        return 0;
      }
    
      if (verificarNumero(entrada))
      {
        printf("São numéricos.\n");
      }
      else
      {
        printf("Não são numéricos.\n");
        return 0;
      }
    
      int i = 0;
      long data[3];
      const char delimitador[2] = "/";
      char *token = strtok(entrada, delimitador);
    
      // Alimenta o vetor de inteiros
      while (token != NULL)
      {
        data[i++] = strtol(token, NULL, 10);
        token = strtok(NULL, delimitador);
      }
    
      // Realize suas validações. Se alguma não for atingida, retorne '0'
    
      printf("Dia: %d\n", data[0]);
      printf("Mes: %d\n", data[1]);
      printf("Ano: %d\n", data[2]);
    
      // Caso contrário, retorne '1'
    
      return 1;
    }
    
    int main()
    {
      char str[80];
    
      printf("Digite uma data: ");
      gets(str);
    
      printf("%d\n", verificarData(str));
    
      return(0);
    }
    
    '; i++) { if (entrada[i] != '/' && !isdigit(entrada[i])) { return 0; } } return 1; } int verificarData(char *entrada) { const char substring[3] = "//"; if (strstr(entrada, substring) != NULL) { return 0; } if (verificarNumero(entrada)) { printf("São numéricos.\n"); } else { printf("Não são numéricos.\n"); return 0; } int i = 0; long data[3]; const char delimitador[2] = "/"; char *token = strtok(entrada, delimitador); // Alimenta o vetor de inteiros while (token != NULL) { data[i++] = strtol(token, NULL, 10); token = strtok(NULL, delimitador); } // Realize suas validações. Se alguma não for atingida, retorne '0' printf("Dia: %d\n", data[0]); printf("Mes: %d\n", data[1]); printf("Ano: %d\n", data[2]); // Caso contrário, retorne '1' return 1; } int main() { char str[80]; printf("Digite uma data: "); gets(str); printf("%d\n", verificarData(str)); return(0); }
        
    17.06.2017 / 23:39
    -1

    Hello

    I made a simple example of how to validate data using if () The user makes the input, the function, verifies that the data is correct; I hope I have helped with an example

    #include <stdio.h>
    
    int main()
    {
        int dd, mm, yy;
    
        printf("Enter date (DD/MM/YYYY format): ");
        scanf_s("%d/%d/%d", &dd, &mm, &yy);
    
        //check year
        if (yy >= 1900 && yy <= 9999)
        {
            //check month
            if (mm >= 1 && mm <= 12)
            {
                //check days
                if ((dd >= 1 && dd <= 31) && (mm == 1 || mm == 3 || mm == 5 || mm == 7 || mm == 8 || mm == 10 || mm == 12))
                    printf("Date is valid.\n");
                else if ((dd >= 1 && dd <= 30) && (mm == 4 || mm == 6 || mm == 9 || mm == 11))
                    printf("Date is valid.\n");
                else if ((dd >= 1 && dd <= 28) && (mm == 2))
                    printf("Date is valid.\n");
                else if (dd == 29 && mm == 2 && (yy % 400 == 0 || (yy % 4 == 0 && yy % 100 != 0)))
                    printf("Date is valid.\n");
                else
                    printf("Day is invalid.\n");
            }
            else
            {
                printf("Month is not valid.\n");
            }
        }
        else
        {
            printf("Year is not valid.\n");
        }
    
        return 0;
    } 
    
        
    17.06.2017 / 18:19