【C程序】字符串拆分子串传入传出接口函数×2

本文介绍了两个C语言函数,用于高效地拆分字符串。第一个函数`split_string`使用指针操作,针对以空格分隔的字符串进行拆分。第二个函数`strtok_string`允许通过参数传递分隔符,能灵活处理不同分隔符的字符串拆分。在`main`函数中,分别展示了这两个函数的使用和输出效果。
摘要由CSDN通过智能技术生成
/* 优点:效率高,纯指针操作 */
#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
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

姜源Jerry

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值