All submissions

Parse GPS String for Time and Coordinates

 

#include <stdio.h>
#include <string.h>

void parse_gprmc(char *nmea) {
    // Your logic here
    char *fields[7];

    // Tokenize by comma
    char *token = strtok(nmea, ",");
    int index = 0;
    while (token != NULL && index < 7) 
    {
        fields[index++] = token;
        token = strtok(NULL, ",");
    }

    // Extract time
    char *time = fields[1];
    printf("Time: %.2s:%.2s:%.2s\n", time, time+2, time+4);

    // Latitude
    printf("Latitude: %s %s\n", fields[3], fields[4]);

    // Longitude
    printf("Longitude: %s %s", fields[5], fields[6]);
}

int main() {
    char nmea[100];
    fgets(nmea, sizeof(nmea), stdin);
    parse_gprmc(nmea);
    return 0;
}

 

 

 

 

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