Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char fields[7][10];
    int count = 0;
    int idx = 0;
    while (*nmea != '\0') {
        if (*nmea == ',') {
            fields[count++][idx] = '\0';
            idx = 0;
            nmea++;
            continue;
        }
        fields[count][idx++] = *nmea;
        nmea++;
    }
    fields[count++][idx] = '\0';
    printf("Time: ");
    for (int i = 0; fields[1][i] != '\0'; i++){
        if (i != 0 && i%2 == 0)
            printf(":");
        printf("%c", fields[1][i]);
    }
    printf("\nLatitude: %s %s", fields[3], fields[4]);
    printf("\nLongitude: %s %s", fields[5], fields[6]);
}

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