qfedu-c-level/day11/d17.c

26 lines
638 B
C
Raw Normal View History

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
int main()
{
char str[100] = "小明:21,,,.男.女,北京:haidian";
int i = 0;
char *delim = ":,."; // 以冒号、逗号、点号作为分隔符
char *ret[10];
ret[i] = strtok(str, delim); // 返回第一次切割的结果
while (ret[i] != NULL)
{
i++;
// 第二次及以后的切割要传入 NULL
ret[i] = strtok(NULL, delim);
}
char *title[10] = {"姓名", "年龄", "性别", "爱好", "地址", "区域"};
for (int j = 0; j < i; j++)
{
printf("%s: %s\n", title[j], ret[j]);
}
return 0;
}