All submissions

Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char *pos = nmea ;
    int cnt = 0 ;
    while(*nmea)
    {
        if(*nmea == ',')
        {
            if(cnt == 0)
            {
                pos = nmea+1;
                cnt++;
            }
            else if(cnt == 1)
            {
                printf("Time: %.2s:%.2s:%.2s\n",pos,pos+2,pos+4);
                pos = nmea+1;
                cnt++;
            }
            else if(cnt == 2)
            {
                pos = nmea+1;
                cnt++;
            }
            else if(cnt == 3)
            {
                //pos = nmea+1;
                cnt++;
            }
            else if(cnt == 4)
            {
                printf("Latitude: ");
                while(pos != nmea)
                {
                    printf("%c",(*pos == ',')? ' ' : *pos);
                    pos++;
                }
                printf("\n");

                pos = nmea+1;
                cnt++;
            }
            else if(cnt == 5)
            {
                //pos = nmea+1;
                cnt++;
            }
        }
        nmea++;
    }

    printf("Longitude: ");
    while(pos != nmea)
    {
        printf("%c",(*pos == ',')? ' ' : *pos);
        pos++;
    }
            
 
}

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

Solving Approach

 

 

 

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