Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {

    char tokens[10][20];
    int i = 0;
    int row = 0;
    int col = 0;

    while(nmea[i] != '\0'){
        if(nmea[i] != ','){
            tokens[row][col] = nmea[i];
            col++;
        }
        else if(nmea[i] == ','){
            tokens[row][col] = '\0';
            row++;
            col = 0;
        }
        i++;
    }
    tokens[row][col] = '\0';
    printf("Time: ");
    for(int j=0; j<6;j++){
        if(j == 2 || j== 4){
            printf(":");
            printf("%c",tokens[1][j]);
        }
        else 
            printf("%c",tokens[1][j]);
    }
    printf("\nLatitude: %s %s\n",tokens[3],tokens[4]);
    printf("Longitude: %s %s\n",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