102. Parse GPS String for Time and Coordinates

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_gprmc(char *nmea) {
    char *token = strtok(nmea, ",");
    int field = 0;

    while (token && field < 7) {
        switch (field) {
            case 1: // Time
                printf("Time: %c%c:%c%c:%c%c\n",
                       token[0], token[1],
                       token[2], token[3],
                       token[4], token[5]);
                break;
            case 3: // Latitude
                printf("Latitude: %s ", token);
                break;
            case 4: // N/S
                printf("%s\n", token);
                break;
            case 5: // Longitude
                printf("Longitude: %s ", token);
                break;
            case 6: // E/W
                printf("%s\n", token);
                break;
        }
        token = strtok(NULL, ",");
        field++;
    }
}


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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote