You are viewing a single comment's thread. Return to all comments →
char a[1000][10]; int index = 0;
int check(char c[]){ for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P'){ return 1; } } return 0; }
char *coppy(char c[]){ char tmp = (char)malloc(sizeof(strlen(c) + 1)); int j = 0; for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P' || c[i] == 'A'){ break; } else { tmp[j] = c[i]; j++; } } tmp[j] = '\0'; return tmp; }
int main(){ char c[1001]; fgets(c, 1001, stdin); c[strlen(c) - 1] = '\0'; char *token = strtok(c, ":"); //tach tu while(token != NULL){ strcpy(a[index], token); index++; token = strtok(NULL, ":"); } int hour = atoi(a[0]); if(check(a[index - 1]) == 1){ if(hour == 12){ printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } else { hour += 12; printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } } else { if(hour == 12){ printf("00:%s:%s", a[1], coppy(a[index - 1])); } else { printf("%s:%s:%s", a[0], a[1], coppy(a[index - 1])); } } return 0; }
Seems like cookies are disabled on this browser, please enable them to open this website
Time Conversion
You are viewing a single comment's thread. Return to all comments →
include
include
include
include
char a[1000][10]; int index = 0;
int check(char c[]){ for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P'){ return 1; } } return 0; }
char *coppy(char c[]){ char tmp = (char)malloc(sizeof(strlen(c) + 1)); int j = 0; for(int i = 0; i < strlen(c); i++){ if(c[i] == 'P' || c[i] == 'A'){ break; } else { tmp[j] = c[i]; j++; } } tmp[j] = '\0'; return tmp; }
int main(){ char c[1001]; fgets(c, 1001, stdin); c[strlen(c) - 1] = '\0'; char *token = strtok(c, ":"); //tach tu while(token != NULL){ strcpy(a[index], token); index++; token = strtok(NULL, ":"); } int hour = atoi(a[0]); if(check(a[index - 1]) == 1){ if(hour == 12){ printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } else { hour += 12; printf("%d:%s:%s", hour, a[1], coppy(a[index - 1])); } } else { if(hour == 12){ printf("00:%s:%s", a[1], coppy(a[index - 1])); } else { printf("%s:%s:%s", a[0], a[1], coppy(a[index - 1])); } } return 0; }