Parse GPS String for Time and Coordinates

Code

#include <stdio.h>
#include <string.h>
#define NMEA_HEADER_LEN 7

void parse_gprmc(char *nmea) {
    // Your logic here
    char *ch=nmea+NMEA_HEADER_LEN;
    printf("Time: ");
        for(int j=0;j<6;j++){
            printf("%c",*ch);
            ch++;
            if((j==1)||(j==3)){
                printf(":");
            }
        }printf("\n");


    printf("Latitude: ");
        ch += 3; 
        while(*ch!=','){
            printf("%c",*ch);
            ch++;
        }
        printf(" ");
        ch++;
        printf("%c",*ch);
        printf("\n");

    printf("Longitude: ");
        ch+=2;
        while(*ch!=','){
            printf("%c",*ch);
            ch++;
        }
        printf(" ");
        ch++;
        printf("%c",*ch);
}

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

Solving Approach

 

 

 

Upvote
Downvote
Loading...

Input

$GPRMC,123519,A,4807.038,N,01131.000,E

Expected Output

Time: 12:35:19 Latitude: 4807.038 N Longitude: 01131.000 E