Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    char *word[7];
    int index = 0;
    char* delimit = ",";
    char* tmp;
    tmp = strtok(nmea, delimit);
    while(tmp != NULL){
        word[index++]=tmp;
        tmp = strtok(NULL, delimit);
    }
    if(index < 7)
       return; 
    
    printf("Time: %c%c:%c%c:%c%c\n",word[1][0],word[1][1],word[1][2],word[1][3],word[1][4],word[1][5]);
    printf("Latitude: %s %s\n",word[3],word[4]);
    printf("Longitude: %s %s\n",word[5],word[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