#include <stdio.h>
#include <string.h>
void parse_time(char *tokens){
printf("Time: ");
for (int i=0; tokens[i] != '\0'; i++){
if (i%2 == 0 && i != 0){
printf(":");
}
printf("%c", tokens[i]);
}
printf("\n");
}
void parse_latitude(char *tokens){
printf("Latitude: %s ", tokens);
}
void parse_longitude(char *tokens){
printf("Longitude: %s ", tokens);
}
void parse_gprmc(char *nmea) {
int count = 0;
char *tokens = strtok(nmea, ","); // inserts \0 for each delim
while (tokens != NULL) {
if (count == 1){
parse_time(tokens);
}
else if (count == 3){
parse_latitude(tokens);
}
else if (count == 4){
printf("%s\n", tokens);
}
else if (count == 5){
parse_longitude(tokens);
}
else if (count == 6){
printf("%s\n", tokens);
}
count++;
tokens = strtok(NULL, ","); // use str from last time
}
}
int main() {
char nmea[100];
fgets(nmea, sizeof(nmea), stdin);
parse_gprmc(nmea);
return 0;
}
Input
$GPRMC,123519,A,4807.038,N,01131.000,E
Expected Output
Time: 12:35:19 Latitude: 4807.038 N Longitude: 01131.000 E