一、利用strtok()函数进行分割函数头文件#include
#include
#include
int main()
{
char str[] = "abc.def.12";
char *index = NULL;
char id[128] = {0};
index = strtok(str, ".");
while (index != NULL)
{
strncpy(id, index, sizeof(id));
index = strtok(NULL, ".");
}
printf("%s\n", id);
}
编译输出:
test# gcc 1.c -o out
test# ./out
12
二、利用正则表达式实现函数原型:int sscanf (char *str, char * format [, argument, …]);与scanf()区别,scanf的输入是在键盘输入的,而sscanf()是在用户定义的缓冲区获取的固定格式的数据。返回值:读取成功的参数个数,失败是-1
#include
int main()
{
char str[] = "123@qq.com";
int b;
char c[10];
char d[10];
int n=sscanf(str, "%d@%[a-z].%[a-z]", &b, c, d);
printf("用户名%d\n", b);
printf("邮箱类型%s\n", c);
printf("第三部分%s\n", d);
printf("返回值%d\n", n); //读取成功的参数个数
}
编译输出:
test# gcc 2.c -o out
test# ./out
用户名123
邮箱类型qq
第三部分com
返回值3
转自:https://blog.csdn.net/qq_45313714/article/details/116044440
参考:字符分割函数strtok