Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    int j=0,k=0;
    char hour[3];
    char min[3];
    char sec[3];
    char tokens[10][100];
    for(int i=0;nmea[i]!='\0';i++){
        if(nmea[i]==','){
            tokens[j][k] = '\0';
            j++;
            k=0;
        }
        else{
            tokens[j][k] = nmea[i];
            k++;
        }
    }
    tokens[j][k] = '\0';
    for(int p=0;p<2;p++){
        hour[p] = tokens[1][p];
        min[p] = tokens[1][p+2];
        sec[p] = tokens[1][p+4];
    }
    printf("Time: %s:%s:%s\n",hour,min,sec);
    printf("Latitude: %s %s\n",tokens[3],tokens[4]);
    printf("Longitude: %s %s",tokens[5],tokens[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