Run Code
|
API
|
Code Wall
|
Misc
|
Feedback
|
Login
|
Theme
|
Privacy
|
Patreon
time conversion1
//gcc 5.4.0 #include <stdio.h> #include <stdlib.h> #include <string.h> struct Map { //or use typedef struct and then struct as Map , then use Map map; //struct var int key[12]; int value[12]; }map; char* convertTime(char s[], struct Map map) { int length = strlen(s); int initLength = length - 2; char t[3],l[3],resultTime[8]; for (int i=0; i<2; i++) { t[i] = s[i]; //get the first two chars in string } for (int j=initLength,i=0; j<length; j++,i++) { l[i] = s[j]; //get the last two chars in string } t[2] = '\0'; l[2] = '\0'; //make them string by appending null char int numT = atoi(t); //convert it to integer as we need to compare int newTime = 0, cmpResult = 1; printf("t = %d\n", numT); printf("l = %s\n",l); cmpResult = strcmp(l,"PM"); if (cmpResult == 0) { //convert into 24hr format for (int i=1; i<13; i++) { if (numT == map.key[i]) { printf("came here1\n"); newTime = map.value[i]; printf("newTime = %d\n",newTime); } } } else { //if the time is in "AM" then no need to convert ,just remove AM substring for (int i=0; i<initLength; i++) { resultTime[i] = s[i]; //get the first two chars in string } resultTime[8] = '\0'; if ((s[0] == '1') && (s[1] == '2')) { resultTime[0] = '0'; resultTime[1] = '0'; } } strcpy(s,resultTime); return s; } int main(void) { printf("Hello, world!\n"); int initVal = 13; //insert values to the map //struct Map map; //either this way or in definition declare variable for (int i=0,j=1; i<13; i++,j++) { map.key[i] = j; map.value[i] = initVal; initVal++; } //printf("map.key = %d \t map.value = %d\n",map.key,map.value); char str[] = "01:40:22AM"; char* time = convertTime(str, map); printf("24hr format : %s\n",time); return 0; }
run
|
edit
|
history
|
help
0
String copy
object-oriented C by Henry Kroll III www.thenerdshow.com
Lab 5 HW Spring 2017 v0.5
lab2
Anutter juan
Lp
ATM
20171103_factorial
709 lab 3.1
No warnings!!!