#include <stdio.h> #include <string.h> void parse_gprmc(char *nmea) { // Your logic here char *fields[7]; int i = 0; // Split the string by commas and store first 7 fields char *token = strtok(nmea, ","); while(token != NULL && i < 7) { fields[i++] = token; token = strtok(NULL, ","); } if(i < 7) { printf("Invalid GPRMC string\n"); return; } // Extract time char *raw_time = fields[1]; char hh[3], mm[3], ss[3]; hh[0] = raw_time[0]; hh[1] = raw_time[1]; hh[2] = '\0'; mm[0] = raw_time[2]; mm[1] = raw_time[3]; mm[2] = '\0'; ss[0] = raw_time[4]; ss[1] = raw_time[5]; ss[2] = '\0'; printf("Time: %s:%s:%s\n", hh, mm, ss); printf("Latitude: %s %s\n", fields[3], fields[4]); printf("Longitude: %s %s\n", fields[5], fields[6]); } 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