Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char t[20];
    char l[20];
    char l1[20];
    int i,j,y;
    j=2;
    for(i=7,y=0;i<13;i++,y++){
        if(y==j){
            t[y]=':';
            j=j+3;
            y++;
        }
        t[y]=nmea[i];
    }
    t[y]='\0';
    printf("Time: %s\n",t);
    for(i=16,y=0;i<=25;i++,y++){
        if(nmea[i]==','){
            l[y]=' ';
        }
        else
        l[y]=nmea[i];
    }
    l[y]='\0';
    printf("Latitude: %s\n",l);
    for(i=27,y=0;i<=37;i++,y++){
        if(nmea[i]==','){
            l1[y]=' ';
        }
        else
        l1[y]=nmea[i];
    }
    l1[y]='\0';
    printf("Longitude: %s",l1);
}

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