Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char tokens[7][10];
    char delimiter = ',';
    int row = 0;
    int col = 0;

    // Step 1 tokenize each sub string
    while (row < 7) {
        if (*nmea == delimiter || *nmea == 0) {
            tokens[row][col] = 0;
            row++;
            col = 0;
        } else {
            tokens[row][col++] = *nmea;
        }
        nmea++;
    }

    // step 2 print time in 00:00:00 format
    printf("Time: %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]);
    // step 3 print lat & N/S
    printf("Latitude: %s %c\n", tokens[3], tokens[4][0]);
    // step 4 print lon & E/W
    printf("Longitude: %s %c\n", tokens[5], tokens[6][0]);
}

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