Parse GPS String for Time and Coordinates

Code

#include <stdio.h>
#include <string.h>

void parse_gprmc(char *nmea) {
    // Your logic here
    int i=7,j=0;
    char time[101]={0},latitude[101]={0},longitude[101]={0};
    while(nmea[i]!='\0'){
        if(i>=7 && i<=12){
            time[j++] = nmea[i];
            if(j==2||j==5) {
                time[j++]=':';
                
            }
        }
        else if(i>=16 && i<=25){
            if(nmea[i]==',') latitude[i-16] = ' ';
            else {
                latitude[i-16]=nmea[i];
            }
        }
        else {
            if(nmea[i]==',') longitude[i-26]=' ';
            else longitude[i-26] = nmea[i];
        }
        i++;
    }
    time[8] ='\0';
    latitude[11]='\0';
    longitude[13]='\0';
    printf("Time: %s\n",time);
    printf("Latitude: %s\n",latitude);
    printf("Longitude: %s\n",longitude+1);
}

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