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 tokens[10][20];
    int i = 0;
    int r = 0;
    int c = 0;

    while(nmea[i] != '\0' && nmea[i] != '\n'){
        if(nmea[i] != ',' && nmea[i] != ' '){
            tokens[r][c] = nmea[i];
            c++;
            i++;
        }
        else{
           tokens[r][c] = '\0';
           r++;
           c = 0;
           i++;
        }
    }
        tokens[r][c] = '\0';

        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]);

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote