Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    char token[10][20];
    int field = 0;
    int pos = 0;

    for(int i=0;nmea[i]!='\0' && field <7;i++){
        if(nmea[i]!=',' && nmea[i]!='\n'){
            token[field][pos++] = nmea[i];
        }
        else{
            token[field][pos]='\0';
            field++;
            pos=0;
        }
    }

    char *time_str = token[1];
    printf("Time: %.2c%.2c:%.2c%.2c:%.2c%.2c\n",
           time_str[0], time_str[1],
           time_str[2], time_str[3],
           time_str[4], time_str[5]);

    printf("Latitude: %s %s\n", token[3], token[4]);

    printf("Longitude: %s %s\n", token[5], token[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