Parse GPS String for Time and Coordinates

Code

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

#define NB_ITEM 7
#define LENGTH_ITEM 20

void parse_gprmc(char *nmea) {
    char nmeaItem[NB_ITEM][LENGTH_ITEM];
    int idxItem = 0;
    int idxCaracItem = 0;
    for(int i=0;nmea[i]!='\0';i++){
        if(nmea[i]==','){
            nmeaItem[idxItem][idxCaracItem] = '\0';
            idxCaracItem = 0;
            idxItem++; 
        }
        else {
            nmeaItem[idxItem][idxCaracItem] = nmea[i],idxCaracItem++;
            if(idxItem == 1){
                if(idxCaracItem == 2 || idxCaracItem == 5 ) nmeaItem[idxItem][idxCaracItem] = ':',idxCaracItem++;
                }
            }
    }
    nmeaItem[idxItem][idxCaracItem] = '\0';
    printf("Time: %s\nLatitude: %s %s\nLongitude: %s %s\n",nmeaItem[1],nmeaItem[3],nmeaItem[4],nmeaItem[5],nmeaItem[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