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>

// modified split string limited to 'count'
void split_string(const char *str, char delimiter, char tokens[10][20], uint8_t count) {
    // Your logic here
    int i=0, j=0, k=0;

    while ((str[i] != delimiter) && (str[i] != '\0') && (k < count)) {
        j=0;
        while ((str[i+j] != delimiter) && (str[i+j] != '\0'))
        {
            tokens[k][j] = str[i+j];
            j++;
        }
        tokens[k][j] = '\0';
        k++;
        i = i + j +1;
    }
}


void parse_gprmc(char *nmea) {
    // Your logic here
    char tokens[7][20];
    uint8_t count=7;
    split_string(nmea, ',', tokens, count);
    printf("Time: %C%C:%C%C:%C%C\n", 
        tokens[1][0], tokens[1][1], tokens[1][2], tokens[1][3], tokens[1][4], tokens[1][5]);

    printf("Latitude: %s %s\n", tokens[3], tokens[4]);
    printf("Longitude: %s %s\n", tokens[5], tokens[6]);
    return;
}

int main() {
    char nmea[100];
    fgets(nmea, sizeof(nmea), stdin);
    parse_gprmc(nmea);
    return 0;
}

Solving Approach

Use a slightly modified function written in an earlier exercise to extract tokens from string

modify the function to limit extraction to seven fields

then extract the tokens

and print time, latitude and longitude as extracted

 

 

Was this helpful?
Upvote
Downvote