#include <stdio.h>
#include <string.h>
void parse_gprmc(char *str) {
int field = 0;
int time = 0;
int len = strlen(str);
printf("Time: ");
for (int i = 0; i < len; i++) {
if (str[i] == ',') {
field++;
// Add labels when we hit specific fields
if (field == 3) printf("\nLatitude: ");
if (field == 4) printf(" "); // Space before N/S
if (field == 5) printf("\nLongitude: ");
if (field == 6) printf(" "); // Space before E/W
if (field == 7) break; // Stop after the 7th field
}
else {
if (field == 1) {
putchar(str[i]);
time++;
if (time == 2 || time == 4) {
printf(":");
}
}
else if (field >= 3 && field <= 6) {
putchar(str[i]);
}
}
}
printf("\n");
}
int main() {
char nmea[100];
fgets(nmea, sizeof(nmea), stdin);
parse_gprmc(nmea);
return 0;
}
Solving Approach
Upvote
Downvote
Loading...
Input
$GPRMC,123519,A,4807.038,N,01131.000,E
Expected Output
Time: 12:35:19
Latitude: 4807.038 N
Longitude: 01131.000 E