#include <stdio.h>
#include <string.h>
void parse_gprmc(char *nmea) {
char tokens[20][20];
int i = 0, j = 0, top = 0;
// Remove newline
nmea[strcspn(nmea, "\n")] = '\0';
// Split by comma
while (nmea[i]) {
if (nmea[i] != ',') {
tokens[top][j++] = nmea[i];
} else {
tokens[top][j] = '\0';
top++;
j = 0;
}
i++;
}
tokens[top][j] = '\0';
top++;
// Check GPRMC
if (strcmp(tokens[0], "$GPRMC") != 0) {
printf("Not a GPRMC sentence\n");
return;
}
// Time hhmmss
printf("Time: ");
printf("%c%c:%c%c:%c%c\n",
tokens[1][0], tokens[1][1],
tokens[1][2], tokens[1][3],
tokens[1][4], tokens[1][5]);
// Latitude
printf("Latitude: %s %s\n", tokens[3], tokens[4]);
// Longitude
printf("Longitude: %s %s\n", tokens[5], tokens[6]);
}
int main() {
char nmea[100];
fgets(nmea, sizeof(nmea), stdin);
parse_gprmc(nmea);
return 0;
}
Input
$GPRMC,123519,A,4807.038,N,01131.000,E
Expected Output
Time: 12:35:19 Latitude: 4807.038 N Longitude: 01131.000 E