Use Strtok to seperate tokens using 2d array

0

I am messing around with C and want to try and do the following off instructions given. I understand that using a 2-d array isn't necessary but since the instructions say so below It is non negotiable to do otherwise. so a User enters a date in the following format

Saturday, July 8, 2017, 22:14:10

and I want to Use strtok to separate tokens and display output as follows,

Month: July
Day: 8
Year: 2017
Hour: 22
Minute: 14
Second: 10

I need to do this by Creating an array of strings (or a 2-D string) and copy the tokens such that s[0]=”July”, s[1]=”8” and s[5]=”10”. Use a for loop to display s[0] to s[5]. I have this so far:

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

//"Saturday, July 8, 2017, 22:14:10"

int main(void) {
    char date[8][20];
    char *order[] = {"Month", "Day", "Year", "Hour", "Minute", "Second" };
    printf(" Example: Saturday, July 8, 2017, 22:14:10 \n Enter date seperated by commas in following format: \n");
    char text[64];
    scanf(" %s, text");

    char* delims = " ,:";
    char* token = strtok(text,delims);
    char** label =  order;
    int r = 0;
    while (token){

        strcpy(date[r],token);
        if (!date[r][0]) continue;
        printf("%-8s: %s\n ",*label,date[r]);
    token = strtok(NULL,delims);
    label++;
    r++;
    }
    return 0;
}

This is my output when I run the code

Example: Saturday, July 8, 2017, 22:14:10 Enter date seperated by commas in following format: Monday, August 1, 2012, 21:12:9 Month : éq(u Process finished with exit code -1073741819 (0xC0000005)

What Am I doing wrong. Please help!

c
token
asked on Stack Overflow Jun 20, 2020 by Vicente Nuñez • edited Jun 20, 2020 by Vicente Nuñez

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0