How to get C language string specific characters or numbers

Asked 2 years ago, Updated 2 years ago, 35 views

Hello, I am a university student studying C language.

1995/11/10, Hot Six, Seoul

Is there a way to take the date of the year and date in the beginning of this sentence as a number and store it in a variable?

string c

2022-09-21 17:34

1 Answers

Compiler: gcc version 5.4.0 20160609 (Ubuntu 5.4.0-6ubuntu1~16.04.4)

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

int main () {
    char str[80] = "1995/11/10, Hot Six, Seoul";
    char yyyymmdd[11];
    const char split[2] = "/";
    char *token;

    int year, month, day;

    memcpy(yyyymmdd, str, 10);
    printf("%s\n", yyyymmdd);

    token = strtok(yyyymmdd, split);

    year = atoi(token);
    printf("%d\n", year);

    token = strtok(NULL, split);
    month = atoi(token);
    printf("%d\n", month);

    token = strtok(NULL, split);
    day = atoi(token);
    printf("%d\n", day);

    return 0;
}

Results:
1995/11/10
1995
11
10


2022-09-21 17:34

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.