All submissions

Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    char gps_data[7][20];
    int index = 0;
    int data_index = 0;
    while (*nmea && index <= 6)
    {
        if (*nmea == ',')
        {
            gps_data[index][data_index] = 0;
            index++;
            data_index = 0;
        }
        else
        {
            gps_data[index][data_index] = *nmea;
            data_index++;
        }
        nmea++;
    }
    gps_data[index][data_index] = 0;
    //for (int i = 0; i < 7; i++) printf("%s\n", gps_data[i]);
    printf("Time: %.2s:%.2s:%.2s\n", gps_data[1], gps_data[1] + 2, gps_data[1] + 4);
    printf("Latitude: %s %c\n", gps_data[3], gps_data[4][0]);
    printf("Longitude: %s %c\n", gps_data[5], gps_data[6][0]);
}

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

Solving Approach

 

 

 

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