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 herevoid parse_gprmc(char *nmea) {

    char time[20];

    char status[5];

    char lat[20];

    char ns[5];

    char lon[20];

    char ew[5];

    // Extract first 7 fields

    sscanf(nmea,

           "$GPRMC,%[^,],%[^,],%[^,],%[^,],%[^,],%[^,]",

           time, status, lat, ns, lon, ew);

    // Print formatted UTC time

    printf("Time: %.2s:%.2s:%.2s\n",

           time, time + 2, time + 4);

    // Print latitude and longitude

    printf("Latitude: %s %s\n", lat, ns);

    printf("Longitude: %s %s\n", lon, ew);


    
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote