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 *tok;
   char *tok = strtok(nmea,",");
    int field =0;
    // Your logic here


    while( tok != NULL)
    {
        // printf("%s",tok);
        if(field == 1)
        {
            printf("Time: %.2s:%.2s:%.2s\n",tok,tok+2,tok+4);
        }
        if(field == 3)
        {
            printf("Latitude: %s",tok);
        }
        if(field == 4)
        {
            printf(" %s\n",tok);
        }
        if(field == 5)
        {
            printf("Longitude: %s",tok);
        }
        if(field == 6)
        {
            printf(" %s\n",tok);
        }
        field++;
        tok = strtok(NULL, ",");
    }
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote