#include <iostream>
#include <cstring>
#include <cstdlib>
using namespace std;
int main()
{
char str[] = "CMD:MOVE,X=10,Y=20";
char* ptr = strtok(str, ",");
char command[20] = {0};
int Xdata = 0;
int Ydata = 0;
bool hasCmd = false;
bool hasX = false;
bool hasY = false;
while (ptr != NULL)
{
if (strncmp(ptr, "CMD:", 4) == 0)
{
strcpy(command, ptr + 4);
hasCmd = true;
}
else if (strncmp(ptr, "X=", 2) == 0)
{
Xdata = atoi(ptr + 2);
hasX = true;
}
else if (strncmp(ptr, "Y=", 2) == 0)
{
Ydata = atoi(ptr + 2);
hasY = true;
}
ptr = strtok(NULL, ",");
}
// 결과 검증
if (!hasCmd)
{
printf("Error: Missing CMD\n");
return 1;
}
if (!hasX)
{
printf("Error: Missing X value\n");
return 1;
}
if (!hasY)
{
printf("Error: Missing Y value\n");
return 1;
}
// 출력
printf("Command: %s\n", command);
printf("X: %d\n", Xdata);
printf("Y: %d\n", Ydata);
return 0;
}
To embed this project on your website, copy the following code and paste it into your website's HTML: