Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char *tokens[6];
    nmea += 7; //skips $GPRMC,
    int count = 0;
    char *start = nmea;

    while(*nmea && count<6){
        if(*nmea==','){
            *nmea = '\0'; //end the current token directly in buffer
            tokens[count++] = start; //assign the starting address to tokens pointer
            start = nmea + 1;
        }
        nmea++;
    }

    if(count<6 && *nmea=='\0'){ //end of string buffer
        tokens[count++] = start;
    }

    printf("Time: %.2s:%.2s:%.2s\n", tokens[0], tokens[0]+2, tokens[0]+4);
    printf("Latitude: %s %s\n", tokens[2], tokens[3]);
    printf("Longitude: %s %s", tokens[4], tokens[5]);
}

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