#include <stdio.h> #include <string.h> void parse_time(const char* str, char* output = NULL) { output[0] = str[0]; output[1] = str[1]; output[2] = output[5] = ':'; output[3] = str[2]; output[4] = str[3]; output[6] = str[4]; output[7] = str[5]; output[8] = '\0'; } void parse_gprmc(char *nmea) { // Your logic here char* token = strtok(nmea, ","); int count = 0; char time[9]; char lat[25]; char lon[25]; while(token != NULL) { if (count == 1) { parse_time(token, time); // printf("->time: %s\n", time); } else if (count == 3) { strcpy(lat, token); } else if (count == 4) { int len = strlen(lat); lat[len] = ' '; lat[len+1] = token[0]; lat[len+2] = '\0'; } else if (count == 5) { strcpy(lon, token); } else if (count == 6) { int len = strlen(lon); lon[len] = ' '; lon[len+1] = token[0]; lon[len+2] = '\0'; } token = strtok(NULL, ","); count++; } printf("Time: %s\n", time); printf("Latitude: %s\n", lat); printf("Longitude: %s", lon); } int main() { char nmea[100]; fgets(nmea, sizeof(nmea), stdin); parse_gprmc(nmea); return 0; }
Test Cases
Test Results
Input
$GPRMC,123519,A,4807.038,N,01131.000,E
Expected Output
Time: 12:35:19 Latitude: 4807.038 N Longitude: 01131.000 E