前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >使用C语言实现字符串分割

使用C语言实现字符串分割

作者头像
全栈程序员站长
发布2022-09-01 09:00:45
1.4K0
发布2022-09-01 09:00:45
举报

大家好,又见面了,我是你们的朋友全栈君。

之前分享了一篇使用C++(std::string 作为容器)进行字符串分割的博文:

https://blog.csdn.net/r5014/article/details/82802664

现在又想用C语言做一个字符串分割的函数,大概功能是这样:

需要分割的字符串“ this is a charactor raw. ” 使用 ‘ ‘分割

分割之后会返回一个char** strv 表示分割出子串str的vector,返回一个int strc表示分割出子串的数量,在使用完毕之后自行释放strv

strv可能是NULL 比如” “使用‘ ’分割之后就是NULL。

以下介绍分割函数splitstr_c()

代码语言:javascript
复制
//* 切割字符串,strv返回字符串数组,strc返回分割之后的字符串数量
void splitstr_c(char* str, char c, char*** strv, int* strc)
{
    int    i = 0;
    int    j = 0;
    int    n = 0;
    int    offset_strv = 0;
    int    offset_font = 0;
    int    offset_back = 0;
    int    str_size = strlen(str);
    char** tstrv = NULL;


    for(i = 0; i < str_size; i++)
    {
		if(i > 0)
		{
			if((str[i] != c) && (str[i - 1] == c))
			{
				n++;
			}
		}
		else
		{
			if(str[0] != c)
			{
				n++;
			}
		}
    }
	if(n == 0)
	{
		 for(i = 0; i < str_size; i++)
		 {
			if(str[i] != c)
			{
				n++;
				break;
			}
		 }
	}
		


    * strc = n;
    tstrv = (char**)malloc(sizeof(char*) * n);
    memset(tstrv, 0, sizeof(char*)*n);

    for(i = 0; i < str_size; i++)
    {
        if(str[i] == c)
        {
            offset_back = i;
            if(offset_back != offset_font)
            {
                n = offset_back - offset_font;
                char* sub_str = (char*)malloc(sizeof(char) * (n + 1)); //* n + 1是为了容纳'\0'
                memset(sub_str, 0, sizeof(char) * (n + 1));
                memcpy(sub_str, str + offset_font, n);
                tstrv[offset_strv] = sub_str;
                offset_strv++;
            }

            offset_font = offset_back + 1;
        }
    }

    if(offset_back < str_size)
    {
        offset_back = str_size;

        if(offset_back != offset_font)
        {
            n = offset_back - offset_font;
            char* sub_str = (char*)malloc(sizeof(char) * (n + 1));
            memset(sub_str, 0, sizeof(char) * (n + 1));
            memcpy(sub_str, str + offset_font, n);
            tstrv[offset_strv] = sub_str;
            offset_strv++;
        }

        offset_font = offset_back + 1;
    }

    * strv = tstrv;
}

顺带给出两个小工具函数:

代码语言:javascript
复制
//* print strv
void print_strv(char** strv, int strc)
{
	if(strc > 0)
	{
		for(int i = 0; i < strc; i++)
		{
			printf("%s\n",strv[i]);
		}
	}
}

//* strv使用完之后根据strc来进行释放。
void free_strv(char** strv, int strc)
{
	if(strc > 0)
	{

		for(int i = 0; i < strc; i++)
		{

			//printf("%s\n",strv[i]);
			free(strv[i]);
		}

		free(strv);
	}
}

让我们来试一下:

代码语言:javascript
复制
char  *text = "   this  is a charactor text.    ";
//char  *text = "000this00is0a0charactor0text.00";
//char  *text = "this is a charactor text.";
//char *text = "a  a a    a     s  ";

char** strv = NULL;
int    strc = 0;

splitstr_c(text, ' ', &strv, &strc);

printf("splitstr_c: %d\n", strc);
print_strv(strv, strc);
free_strv(strv, strc);

结果:

使用C语言实现字符串分割
使用C语言实现字符串分割

自此这个功能就实现了

发布者:全栈程序员栈长,转载请注明出处:https://javaforall.cn/143773.html原文链接:https://javaforall.cn

本文参与 腾讯云自媒体同步曝光计划,分享自作者个人站点/博客。
原始发表:2022年5月2,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体同步曝光计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
相关产品与服务
容器服务
腾讯云容器服务(Tencent Kubernetes Engine, TKE)基于原生 kubernetes 提供以容器为核心的、高度可扩展的高性能容器管理服务,覆盖 Serverless、边缘计算、分布式云等多种业务部署场景,业内首创单个集群兼容多种计算节点的容器资源管理模式。同时产品作为云原生 Finops 领先布道者,主导开源项目Crane,全面助力客户实现资源优化、成本控制。
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档