All submissions

Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char * c_ptr = nmea;
    // Time category
    if(strncmp(c_ptr,"$GPRMC,",6)==0)
        c_ptr+=7;
    char time_buff[9];
    int index = 0;
    while(*c_ptr != ',')
        if(index == 2 || index == 5){
            time_buff[index++] = ':';
        } else {
            time_buff[index++] = *c_ptr++;
        }
    time_buff[index] = '\0';
    printf("Time: %s\n",time_buff);
    
    // move past ,A,
    if(strncmp(c_ptr,",A,",3)==0)
        c_ptr+=3;

    //Latitude
    char lat_buff[11];
    index = 0;
    while(*c_ptr != ',')
        lat_buff[index++] = *c_ptr++;
    // move passed delimiter
    if(*c_ptr == ',')
        c_ptr++;
    lat_buff[index++] = ' ';
    lat_buff[index++] = *c_ptr++;
    lat_buff[index] = '\0';
    printf("Latitude: %s\n",lat_buff);
    //Longitude 
    char long_buff[11];
    index = 0;
    // move passed delimiter
    if(*c_ptr == ',')
        c_ptr++;
    while(*c_ptr != ',')
        long_buff[index++] = *c_ptr++;
    // move passed delimiter
    if(*c_ptr == ',')
        c_ptr++;
    long_buff[index++] = ' ';
    long_buff[index++] = *c_ptr++;
    long_buff[index] = '\0';
    printf("Longitude: %s ",long_buff);        
}

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