Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    char str1[8][15];
    int RawIndex = 0;
    int ColomIndex = 0;
    int index = 0;

    while(nmea[index] != '\0')
    {
        if(nmea[index] == ',')
        {
            str1[RawIndex][ColomIndex] = '\0';
            RawIndex++;
            ColomIndex = 0;
            index++;
        }
        str1[RawIndex][ColomIndex] = nmea[index];
        index++;
        ColomIndex++;
    }

    // for(int i = 0;i<=RawIndex;i++)
    // {
    //     for(int j = 0;str1[i][j]!='\0';j++)
    //     {
    //         printf("%c",str1[i][j]);
    //     }
    //     printf("\n");
    // }
    printf("Time: ");
    for(int i = 0;str1[1][i] != '\0';i++)
    {
        if(i>1 && (i%2 ==0))printf(":");
        printf("%c",str1[1][i]);
    }
    printf("\nLatitude: ");
    for(int i = 0;str1[3][i] != '\0';i++)
    {
        printf("%c",str1[3][i]);
    }
    printf(" %c\nLongitude: ",str1[4][0]);
    for(int i = 0;str1[5][i] != '\0';i++)
    {
        printf("%c",str1[5][i]);
    }
    printf(" %c",str1[6][0]);
}

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