Parse GPS String for Time and Coordinates

Code

#include <stdio.h>
#include <string.h>
#include <stdint.h>
void split_string(const char *str, char delimiter, char tokens[7][100], uint8_t *count) {
    // Your logic here
    int i=0;
    while (*str != '\0') {
        if (*str == delimiter) {
            tokens[*count][i] = '\0';
            i = 0;
            (*count)++;
            str++;
            continue;
        }
        tokens[*count][i] = *str;
        i++;
        str++;
    }
    tokens[*count][i] = '\0';
    (*count)++;
}
void parse_gprmc(char *nmea) {
    // Your logic here
    char tokens[7][100];
    int count = 0;
    split_string(nmea, ',', tokens,(uint8_t*)&count);
    printf("Time: ");
    for (int i=0; i<6; i++) {
        if (i%2==0 && i!=0) printf(":");
        printf("%c",tokens[1][i]);
    }
    printf("\n");
    printf("Latitude: %s %s\n",tokens[3],tokens[4]);
    printf("Longitude: %s %s",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