#include <stdio.h>
#include <string.h>
void parse_gprmc(char *nmea) {
// Your logic here
if (!nmea || *nmea == '\0') return;
char *fields[15]; // Mảng chứa các con trỏ trỏ đến từng trường
int count = 0;
fields[count++] = nmea; // Trường đầu tiên ($GPRMC)
for (char *p = nmea; *p != '\0'; p++)
{
if (*p == ',')
{
*p = '\0'; // "Chặt" chuỗi tại dấu phẩy
fields[count++] = p + 1; // Lưu địa chỉ bắt đầu trường tiếp theo
if (count >= 15) break;
}
}
// Lúc này các trường được truy cập cực nhanh qua fields[index]
// Ví dụ: fields[1] là Time, fields[3] là Latitude...
// In Time theo định dạng hh:mm:ss (giả sử fields[1] có dữ liệu)
if (*fields[1] != '\0')
{
printf("Time: %.2s:%.2s:%.2s", fields[1], fields[1] + 2, fields[1] + 4);
}
printf("\nLatitude: %s %s", fields[3], fields[4]);
printf("\nLongitude: %s %s", fields[5], fields[6]);
}
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