Home > Software engineering >  How do I divide a date into a day, a month and a year in C?
How do I divide a date into a day, a month and a year in C?

Time:02-04

I am new to C and I would like to ask for help to write a function that splits a date that is stored in a string array into a day, a month and a year, each stored in separate integer variables. I appreciate any help.

I defined the following array and variables:

#include <stdio.h>

int main () {

    char Date[] = "02/02/2023";

    int Day, Month, Year;

    }

I expect my variables to contain the following values at the end:

Day = 02

Month = 02

Year = 2023

I know of this function but it only works when the date is entered by the user in the command prompt:

scanf ("%d/%d/%d", &Day, &Month, &Year);

I want my function to work the same but on a date that is stored in an array.

CodePudding user response:

You can use sscanf instead of scanf:

if(sscanf(Date, "%d/%d/%d", &Day, &Month, &Year) == 3) {
    // success
}

Or, if you have a POSIX library, the power of strptime to parse the input may be useful:

#define _XOPEN_SOURCE // Define this before including anything
#include <stdio.h>
#include <time.h>
/*
char *strptime(const char *restrict s, const char *restrict format,
               struct tm *restrict tm);
*/

int main() {
    char Date[] = "02/02/2023 Foo";

    struct tm tp;
    char *res = strptime(Date, "%d/%m/%Y", &tp);

    if (res == NULL) {
        // the time point could not be parsed
        printf("could not parse [%s]\n", Date);
    } else {
        // the time point was parsed ok
        printf("%d-d-d\n", tp.tm_year   1900, tp.tm_mon   1, tp.tm_mday);

        if (*res != '\0') {
            // but there was some more text after the parsed time point:
            printf("rest of string [%s]\n", res);
        }
    }
}

Output:

2023-02-02
rest of string [ Foo]

Demo with validation and normalization

CodePudding user response:

To convert any string containing numbers into integers, you can use the strtol family of functions. In the simplest form:

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

int main (void)
{
  char date[] = "02/02/2023";
  int day, month, year;
  char* endptr;
  char* next;

  next = date;
  day   = strtoul(next, &endptr, 10);
  next = endptr 1;
  month = strtoul(next, &endptr, 10);
  next = endptr 1;
  year  = strtoul(next, &endptr, 10);

  printf("%d %d %d\n", day, month, year);
}

Or use somewhat more advanced but generic form with a loop:

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

int main (void)
{
  char date[] = "02/02/2023";
  int day, month, year;
  int* const items[3] = { &day, &month, &year };
  char* endptr;
  char* next=date;
  
  for(size_t i=0; i<3 && *next!='\0'; i  )
  {
    *items[i] = strtoul(next, &endptr, 10);
    if(endptr == next)
    {
      /* handle errors here */
      return 0;
    }
    next = endptr 1; // could also loop here til finding next digit
  } 
  
  printf("%d %d %d\n", day, month, year);
}
  •  Tags:  
  • c
  • Related