All submissions

Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    
    uint8_t HH=0, MM=0, SS=0;
    float lat=0.0, lon=0.0;
    char lat_dir = ' ', long_dir = ' ';
    char *token;
    token = strtok(nmea,",");
    uint8_t index=0;
    while(token!=NULL)
    {
        if(index==1)
        {
            char char_HH[3];
            char char_MM[3];
            char char_SS[3];

            char_HH[0] =  token[0];
            char_HH[1] =  token[1];

            char_MM[0] =  token[2];
            char_MM[1] =  token[3];

            char_SS[0] =  token[4];
            char_SS[1] =  token[5];

            char_HH[2] = '\0';
            char_MM[2] = '\0';
            char_SS[2] = '\0';


            HH = (uint8_t)atoi(char_HH);
            MM = (uint8_t)atoi(char_MM);
            SS = (uint8_t)atoi(char_SS);
        }

        else if(index==3)
        {
            lat  = atof(token);
        }

        else if(index==4)
        {
            lat_dir = token[0];
        }

        else if(index==5)
        {
            lon  = atof(token);
        }

        else if(index==6)
        {
            long_dir = token[0];
        }

       
        token = strtok(NULL,",");
        index++;
    }


    printf("Time: %02u:%02u:%02u\n",HH,MM,SS);
    printf("Latitude: %08.3f %c\n",lat,lat_dir);
    printf("Longitude: %09.3f %c\n",lon,long_dir);
}

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