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) {
   char tokens[10][20];
   int count = 0;
   int j = 0;

   while(*nmea) {
    if(*nmea == ',') {
        tokens[count][j] = '\0';
        count++;
        j = 0;
    }else {
        tokens[count][j] = *nmea;
        j++;
    }
    nmea++;
   }
   if(j>0) {
    tokens[count][j] = '\0';
    count++;
    j = 0;
   }
   printf("Time: %c%c:%c%c:%c%c\n",tokens[1][0],tokens[1][1],
   tokens[1][2],tokens[1][3],tokens[1][4],tokens[1][5]);
   printf("Latitude: %s %s\n", tokens[3],tokens[4]);
   printf("Longitude: %s %s\n", tokens[5],tokens[6]);
   
}

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

Solving Approach

 

 

 

Was this helpful?
Upvote
Downvote