#include <stdio.h> #include <string.h> void process_time(const char *time, char *actual_time) { char hh[3], mm[3], ss[3]; strncpy(hh, time, 2); hh[2] = '\0'; strncpy(mm, time + 2, 2); mm[2] = '\0'; strncpy(ss, time + 4, 2); ss[2] = '\0'; snprintf(actual_time, 15, "%s:%s:%s", hh, mm, ss); } void parse_gprmc(char *nmea) { char format[10], time[15], lat[15], longit[15], status[15], ns[15], ew[15]; if (sscanf(nmea, "%[^,],%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]", format, time, status, lat, ns, longit, ew) != 7) { printf("Failed to parse NMEA string.\n"); return; } char actual_time[15]; process_time(time, actual_time); printf("Time: %s\n", actual_time); printf("Latitude: %s %s\n", lat, ns); printf("Longitude: %s %s\n", longit, ew); } int main() { char nmea[100]; fgets(nmea, sizeof(nmea), stdin); nmea[strcspn(nmea, "\n")] = '\0'; // Remove newline 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