Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char time[10], lat[15], ns, ew, lon[15];
    int i = 0,j = 0;

    while(nmea[i] != ','){ ///skip $GPRMC
        i++;
    }
    i++; ////skip ','
    j = 0;
    while(nmea[i] != ','){
        time[j++] = nmea[i++]; ////copy time
    }
    time[j] = '\0'; ////null char
    i++;
     while(nmea[i] != ','){
        i++; ////skip status
    }
    i++; ///skip ','
    j = 0;
         while(nmea[i] != ','){
        lat[j++] = nmea[i++];/////copy latitude
    }
    lat[j] = '\0'; ////null char
    i++; ////skip ','

    ns = nmea[i]; /////n/s direction of latituide
    i += 2; ///skip ns and ','

    j = 0;
             while(nmea[i] != ','){
        lon[j++] = nmea[i++]; /////copy longitude
    }
    lon[j] = '\0'; //////null char
    i++;/////skip ','

    ew = nmea[i]; /////e/w direction of longitude

    printf("Time: %.2s:%.2s:%.2s\n", time, time+2, time+4); ///////breaking string at 2-2 chars
    printf("Latitude: %s %c\n", lat, ns);
    printf("Longitude: %s %c\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