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) {
    // Your logic here
    char* fields[7];
    int count = 0;
    char* curr = strtok(nmea,",");
    while((count < 7 )&& (curr != NULL))
    {
        fields[count] = curr;
        curr = strtok(NULL,",");
        count++;
    }
    //check if count <7 error
    if(count < 7)
    {
        printf("error in string parse!");
        return;
    }
    printf("Time: %c%c:%c%c:%c%c\n" , fields[1][0],fields[1][1],fields[1][2],fields[1][3],fields[1][4],fields[1][5]);
    printf("Latitude: %s %s\n" ,fields[3],fields[4]);
    printf("Longitude: %s %s\n" ,fields[5],fields[6]);
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote