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 time[20];
    int t = 0;
    char lati[20];
    int la = 0;
    char longi[20];
    int lo = 0;
    char ns;
    char ew;

    int j = 0;
    for(int i = 0; nmea[i] != '\0'; i++){
        if(nmea[i] == ','){
            j++;
            i++;
        }
        if(j == 1){
            time[t] = nmea[i];
            t++;
        }
        if(j == 3){
            lati[la] = nmea[i];
            la++;
        }
        if(j ==4){
            ns = nmea[i];
        }
        if(j == 5){
            longi[lo] = nmea[i];
            lo++;
        }
        if(j == 6){
            ew = nmea[i];
        }

    }


    printf("Time: ");
    for(int i = 0; i < t; i++){
        if((i == 2) || (i ==4)){
            printf(":");
        }
        printf("%c", time[i]);
    }

    printf("\nLatitude: ");
    for(int i = 0; i< la; i++){
        printf("%c", lati[i]);
    }
    printf(" %c", ns);

    printf("\nLongitude: ");
    for(int i = 0; i < lo; i++){
        printf("%c", longi[i]);
    }
    printf(" %c", ew);

}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote