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
    int myLen = strlen(nmea);
    int myIndex = 0;
    int count = 0;
    char tokens[7][10];
    char myHours[3], myMinutes[3], mySeconds[3], myTime[9];

    for (int i = 0; i < myLen; i++){
        while((*nmea != 0x00) && (*nmea != ',')){
            tokens[count][myIndex++] = *nmea;
            nmea++;            
        }
        tokens[count][myIndex] = 0x00;
        nmea++; 
        myIndex = 0;
        count++;
        if(*nmea == 0x00) break;
    }
    myHours[0] = tokens[1][0];
    myHours[1] = tokens[1][1];
    myHours[2] = 0x00;

    myMinutes[0] = tokens[1][2];
    myMinutes[1] = tokens[1][3];
    myMinutes[2] = 0x00;

    mySeconds[0] = tokens[1][4];
    mySeconds[1] = tokens[1][5];
    mySeconds[2] = 0x00;

    myTime[0] = myHours[0];
    myTime[1] = myHours[1];
    myTime[2] = ':';
    myTime[3] = myMinutes[0];
    myTime[4] = myMinutes[1];
    myTime[5] = ':';
    myTime[6] = mySeconds[0];
    myTime[7] = mySeconds[1];
    myTime[8] = 0x00;

    printf("Time: %s\n", myTime);
    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