dimanche 5 décembre 2021

Problem with if statements, containing token from strtok in C

Here's the task: 1)07/21/2003 2)July 21, 2003

Write a program that reads a date in the first format and prints it in the second format.

I am supposed to use string methods, especially strtok.

Here's my code:

#include <stdio.h>
#include <string.h>

int main()
{
    char date[20];
    char month[20];
    char day[20];
    char year[20];
    char *token;
    char sep[1] = "/";

    printf("Enter the date (MM/DD/YYYY): ");
    gets(date);

    token = strtok(date, sep);

    if (token == "01")
    {
        strcpy(month, "Januray");
    }

    else if (token == "02")
    {
        strcpy(month, "February");
    }

    //continuing in this way
   
    }

    else if (token == "11")
    {
        strcpy(month, "November");
    }

    else
    {
        strcpy(month, "December");
    }

    token = strtok(NULL, sep);
    strcpy(day, token);

    token = strtok(NULL, sep);
    strcpy(year, token);

    printf("%s %s, %s", month, day, year);
}

The problem is that the month part always gives December, which means if statements do not work.

Aucun commentaire:

Enregistrer un commentaire