Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char tokens[10][20];
    int cnt = 0;
    int i = 0, j = 0, k = 0;
    while (nmea[i] != '\0' && nmea[i] != '\n')
    {
        if (nmea[i] == ',')
        {
            tokens[j][k] = '\0';
            j++;
            k = 0;
        }else {
            tokens[j][k++] = nmea[i];
        }
        i++;
    }
    tokens[j][k] = '\0';
    cnt = j + 1;
    char *time = tokens[1];
    char *lat = tokens[3];
    char *ns = tokens[4];
    char *lon = tokens[5];
    char *ew = tokens[6];
    printf("Time: %.2s:%.2s:%.2s\n", time, time + 2, time + 4);
    printf("Latitude: %s %s\n", lat, ns);
    printf("Longitude: %s %s\n", lon, ew);
}

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