Parse GPS String for Time and Coordinates

Code

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

void parse_gprmc( char *name)
{
    char fields[7][20];
    int row =0, col =0, i=0;

    while(name[i]!='\0' && row < 7){
        if(name[i] ==','){
            fields[row][col] = '\0';
            row++;
            col=0;
        }
        else{
            fields[row][col++]=name[i];
        }
        i++;
    }

    fields[row][col] = '\0';

    char *time = fields[1];

    //printf("Time: %c%c:%c%c:%c%c\n", time[0]:time[1], time[2]:time[3], time[4]:time[5]);

    printf("Latitude: %s %s\n", fields[3],fields[4]);

    printf("longitude: %s %s\n", fields[5],fields[6]);

}

// void parse_gprmc1( char *name){


//     int i=0;

//     while(name[i]!=','){
//         i++;
//     }

//     char fields[7];
//     char longitude[20], latitude[20];

//     int j =0;

//     i++;

//     for(; name[i]!=',';i++){

//         if(j< sizeof(fields) -1)// do this check to avoid overflow
//             fields[j++] = name[i];
//     }

//     fields[j]='\0';

//     i+=3;

//     printf("Time: %.2s:%.2s:%.2s\n",fields, fields+2,fields+4);

//     j=0;

//     for(;name[i]!=',';i++){
//         if (j < sizeof(latitude) - 2)// do this check to avoid overflow
//         latitude[j++]=name[i];
//     }

//     latitude[j++] = ' ';
//     latitude[j++]= name[++i];// this works, but reading it is confusing
//     latitude[j]='\0';

//     printf("Latitude: %s\n", latitude);

//     i+=2;
//     j=0;
//     for(;name[i]!=','; i++){
//         if (j < sizeof(longitude) - 2)// do this check to avoid overflow
//         longitude[j++] = name[i];
//     }

//     i++;
//     longitude[j++]=' ';
//     longitude[j++]=name[i];
//     longitude[j]='\0';

//     printf("Longitude: %s", longitude);

// }


// void parse_gprmc(char *nmea) {
//     int i =0;
//     while( nmea[i]!=','){
//         i++;
//     }
//     char fields[7];
//     char Latitude[20], Longitude[20];

//     int j = 0 ;
//     i++;
//     for(; nmea[i]!=','; i++){
//         fields[j++]=nmea[i];
//     }
//     fields[j]='\0';
//     i+=3;
//     printf("Time: %.2s:%.2s:%.2s\n", fields, fields+2, fields+4);
//     j=0;
//     for( ;nmea[i]!=',' ;i++ ){
//         Latitude[j++]= nmea[i];
//     }

//     i++;
    
//     Latitude[j++] = ' ';
//     Latitude[j++] = nmea[i];
//     Latitude[j]='\0';

//     i+=2;

//     printf("Latitude: %s\n",Latitude);

//     j=0;
//     for( ;nmea[i]!=',' ;i++ ){
//         Longitude[j++]= nmea[i];
//     }
//     i++;
//     Longitude[j++] = ' ';
//     Longitude[j++] = nmea[i];
//     Longitude[j]='\0';

//     printf("Longitude: %s",Longitude);
// }

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

int main(){

    char name[100];

    fgets(name, sizeof(name), stdin);
    parse_gprmc(name);
    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