Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    int i=0,k=0,sem=0,col=2,l=0,m=0;
    char time[9];
    char lat[11];
    char lon[12];
    while(nmea[i]!='\0')
    {
        if(i>=7 &&i<=12)
        {
            time[k]=nmea[i];
             k++;
            sem++;
            if(sem==2 && col>0)
            {
                time[k]=':';
                 k++;
                 col--;
                 sem=0;
            } 
        }
       if(i>=16 &&i<=25)
        {
            
            if(nmea[i]!=',')
            {
                lat[l]=nmea[i];
            }
            else
            {
            lat[l]=' ';
            }
            l++;
            
        }

       if(i>=26 &&i<=37)
        {
            
            if(nmea[i]!=',')
            {
                lon[m]=nmea[i];
            }
            else
            {
            lon[m]=' ';
            }
            m++;
            
        }
        i++;
    }
     time[k]='\0';
     lat[l]='\0';
    printf("Time: %s\n",time);
    printf("Latitude: %s\n",lat);
    printf("Longitude:%s\n",lon);
}


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