I am messing around with C and want to try and do the following. 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 have this so far:
#include <stdio.h>
#include <string.h>
//"Saturday, July 8, 2017, 22:14:10"
int main(void) {
char date[6][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;
scanf(" %s, &text");
char* delims = " ,:";
char* token = strtok(text,delims);
char** label = order;
int r = 0;
while (token){
strcpy(date[r],token);
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
Process finished with exit code -1073741819 (0xC0000005)
What Am I doing wrong. Please help!
#include <stdio.h>
#include <string.h>
int main(void) {
char *order[] = {"Day", "Month", "Date", "Year", "Hour", "Minute", "Second"};
char text[] = "Saturday, July 8, 2017, 22:14:10";
char* delims = " ,:";
char* token = strtok(text, delims);
char** label = order;
while(token)
{
printf("%-8s: %s\n", *label, token);
token = strtok(NULL, delims);
label++;
}
return 0;
}
Success #stdin #stdout 0s 4176KB
Day : Saturday
Month : July
Date : 8
Year : 2017
Hour : 22
Minute : 14
Second : 10