102. Parse GPS String for Time and Coordinates

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_gprmc(char *nmea) {
    char token[10][20];
    uint8_t j = 0;
    uint8_t count = 0;
    while(*nmea) {
        if(*nmea == ','){
            token[count][j] = '\0';
            j= 0;
            count++;
        } else {
            token[count][j] = *nmea;
            j++;
        }
        nmea++;
    }
    token[count][j] = '\0';
    count++;
   printf("Time: %c%c:%c%c:%c%c\n", token[1][0],
   token[1][1], token[1][2], token[1][3], token[1][4], token[1][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

 

 

 

Was this helpful?
Upvote
Downvote