102. Parse GPS String for Time and Coordinates

Back To All Submissions
Previous Submission
Next Submission

Code

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

void parse_gprmc(char *nmea) {
    // Your logic here
    int row = 0,col = 0;
    char str[6][30] ={0};
    while(*nmea != '\0'){
        if(*nmea == ','){
           str[row][col] = '\0';
            row++;
            col = 0;
            nmea++;
            continue;
        }else{
            str[row][col++] = *nmea;
            
        }
        nmea++;
    }
    char *t = str[1];
    printf("Time: %.2s:%.2s:%.2s\n",t,t+2,t+4);
    printf("Latitude: %s %s\n",str[3],str[4]);
    printf("Longitude: %s %s\n",str[5],str[6]);

    // while(*nmea !=',') { nmea++;}
    // nmea++;
    // printf("Time: ");
    // while(*nmea != ','&& *nmea != '/0'){
    //     if(*nmea >= '0' || *nmea <= '9'){
    //         if(count==2){
    //             count = 0;
    //             printf(":");
    //         }
    //        printf("%d",(*nmea - '0'));
    //         nmea++;
    //         count++; 
    //     }
    // }
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote