/* 优点:效率高,纯指针操作 */
#include <stdio.h>
#include <ctype.h>
#include <string.h>
#define LEN 10
static int split_string (char *src_str, char dest_str[][LEN])
{
int index = 0;
char *ph = src_str;
char *p = src_str;
if(!src_str)
return -1;
/* *p != NULL && *p != 空格 */
while(*p && isspace(*p) == 0)
{
if(index > 9)
return -1;
if(*(++p) == ',' || *p == '\0' || isspace(*p))
{
memcpy(dest_str[index], ph, (p - ph));
index++;
ph = p+1;
//printf ("dest_str[%d]=%s.\n",index-1,dest_str[index-1]);
}
}
return 0;
}
int main (void)
{
char *p = "111,222,333,aaa,bbb,ccc";
char test
【C程序】字符串拆分子串传入传出接口函数×2
最新推荐文章于 2021-12-26 14:17:11 发布
本文介绍了两个C语言函数,用于高效地拆分字符串。第一个函数`split_string`使用指针操作,针对以空格分隔的字符串进行拆分。第二个函数`strtok_string`允许通过参数传递分隔符,能灵活处理不同分隔符的字符串拆分。在`main`函数中,分别展示了这两个函数的使用和输出效果。
摘要由CSDN通过智能技术生成