#include <stdio.h>
#include <string.h>
int seek_indx(char *str, int indx)
{
while(str[indx] != ',')
{
indx++;
}
return indx;
}
void parse_time(char* str, int start, int end)
{
//printf("parse time : start= %d, end= %d\n", start, end);
char word[9];
int str_indx = start;
for(int i=0; i<9; i+=3)
{
//printf("i=%d\n", i);
word[i] = str[str_indx++];
word[i+1] = str[str_indx++];
word[i+2] = (i+2 == 8) ? '\0' : ':';
}
printf("Time: %s\n", word);
}
void parse_coordinate(char* str, int start, int end, const char* coordinate, char direction)
{
//printf("parse cordinate : start= %d, end= %d %s %c\n", start, end, coordinate, direction);
char word[end - start + 4];
int word_indx = 0;
for(int i=start; i<=end; ++i)
{
word[word_indx++] = str[i];
}
word[word_indx++] = ' ';
word[word_indx++] = direction;
word[word_indx] = '\0';
printf("%s: %s\n", coordinate, word);
}
void parse_gprmc(char *str) {
// Your logic here
int indx = 7;
int time_start = indx;
indx = seek_indx(str, indx);
parse_time(str, time_start, indx-1);
indx++;
// skip A
indx = seek_indx(str, indx);
indx++;
// parse latitude
int latitude_start = indx;
indx = seek_indx(str, indx);
parse_coordinate(str, latitude_start, indx-1, "Latitude", str[indx+1]);
indx++;
// Skip direction
indx = seek_indx(str, indx);
indx++;
// parse longitude
int longitude_start = indx;
indx = seek_indx(str, indx);
parse_coordinate(str, longitude_start, indx-1, "Longitude", str[indx+1]);
}
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