#include <stdio.h>
#include <string.h>
//missed to notice that i cud use default str fn(strtok) :(, its ok, hehe :)
void parse_gprmc(char *nmea) {
// Your logic here
char tokens[10][20];
int count = 0;
int bit_field=0;
int pos=0;
bool first_com=0;
int k=0;
for(int i=0; nmea[i] != '\0'; i++)
{
if(nmea[i] == ',')
{
bit_field++; // to indetify token
}
switch (bit_field)
{
case 1:
{
if(nmea[i] != ',')
{
tokens[count][k++] = nmea[i];
pos++;
if(pos%2 ==0 && pos<6)
tokens[count][k++] =':';
}
break;
}
case 2:
{
if(nmea[i] == ',')
{
tokens[count][k] = '\0';
count += 1;
k=0;
}
break;
}
case 3:
{
if(nmea[i] != ',')
tokens[count][k++] = nmea[i];
break;
}
case 4:
case 6:
{
if(nmea[i] != ',')
{
tokens[count][k++] = ' ';
tokens[count][k++] = nmea[i];
}
break;
}
case 5:
{
if(nmea[i] == ',')
{
tokens[count][k] = '\0';
count += 1;
k=0;
}
if(nmea[i] != ',')
tokens[count][k++] = nmea[i];
break;
}
default:
break;
}
}
tokens[count][k] = '\0'; // terminate last token
count += 1; //to include last word
//printf("%d", count);
printf("Time: %s\n", tokens[0]);
printf("Latitude: %s\n", tokens[1]);
printf("Longitude: %s\n", tokens[2]);
}
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