用C实现字符串分割并返回所有子串
#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <string.h>
char ** split(char *mother, char split_char)
{
char *arry[1024]; //the MAX sub string is 1024 and you can modify it
char *new;
char buf[1024] = {'\0'};
int len, i, j, k, len_sub;
for(i = 0, j = 0, k = 0; i < strlen(mother); i++) //scan the string "mother" to split by "split_char"
{
if(mother[i] == split_char)
{
len_sub = strlen(buf);
printf("sub len is %d\n", len_sub);
if(len_sub > 0)
{
printf("buf str is %s\n", buf);
new = (char *)malloc(len_sub);
strcpy(new, buf);
arry[j] = new;
printf("arry[%d] = %s\n", j, arry[j]);
j++;
memset(buf, '\0', sizeof(buf));
k = 0;
continue;
} else{
continue;
}
}
if(mother[i] != split_char)
{
buf[k] = mother[i];
k++;
}
if((strlen(mother) - 1) == i && strlen(buf) != 0)
{
printf("the last str is made \n");
printf("buf str is %s\n", buf);
len_sub = strlen(buf);
printf("sub len is %d\n", len_sub);
new = (char *)malloc(len_sub);
if(new == NULL)
{
printf("malloc error\n");
}
strcpy(new, buf);
printf("j = %d\n", j);
arry[j] = new;
memset(buf, '\0', sizeof(buf));
printf("arry[%d] = %s\n", j, arry[j]);
j++;
memset(buf, '\0', sizeof(buf));
}
}
new = (char *)malloc(1); //ened indication in the str arry
new[0] = '\0';
arry[j] = new;
i = 0;
printf("in split func print result as below:\n");
while(strlen(arry[i]))
{
printf("%s\n",arry[i]);
i++;
}
printf("ened print\n");
return arry;
}
int main(void)
{
char *s = "abc def ghi jkl mno";
char **ss;
int i;
ss = split(s, ' ');
i = 0;
printf("in main func print result as below:\n");
while(strlen(ss[i]))
{
printf("%s\n",ss[i]);
i++;
}
printf("ened print\n");
return 0;
}
用C实现字符串分割并返回所有子串的更多相关文章
- MSSQLSERVER数据库- 字符串分割函数返回类型表
遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...
- [leetcode]131. Palindrome Partitioning字符串分割成回文子串
Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...
- Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割
ylbtech-Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割 1.返回顶部 1. Java 实例 - 字符串分割 Java 实例 以下实例使用了 split ...
- Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组
// 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // ...
- JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)
字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...
- SQL Server 游标运用:鼠标轨迹字符串分割
一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...
- Oracle 超长字符串分割劈分
Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...
- Python 字符串分割的方法
在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...
- 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。
split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
随机推荐
- IIC 概述之源码仿真
7.1.1 I2C总线简介 1.I2C总线的基本结构 I2C总线由数据线SDA和时钟线SCL构成,每条线都通过上拉电阻接向正电源,所有采用I2C接口标准的器件均并行挂接在总线上,如图7-1所示. I2 ...
- Windows server 2012 各版本 激活方法
Windows server 2012 激活教程 本文包括以下两种版本的激活过程:(注意RC版的是不能激活的!) 1.Windows server 2012 试用版本激活 2.Windows serv ...
- 汉诺塔问题C++实现
大家好,我是小鸭酱,博客地址为:http://www.cnblogs.com/xiaoyajiang 以下进行汉诺塔问题的递归实现 #include <iostream.h> int gb ...
- Cmake调用NSIS(一个可执行文件,其实就是一个编译器)编译NSIS脚本问题研究
技术经理说,可以用Cmake当中的add_custom_command,add_custom_target命令来使用. 我初次研究了下,add_custom_command应该用官方文档中说明的第二种 ...
- Linux企业级项目实践之网络爬虫(20)——扩展成为规则插件模式
为了方便我们爬虫功能的扩展,最好使用插件机制.使用插件技术能够在分析.设计.开发.项目计划.协作生产和产品扩展等很多方面带来好处:(1)结构清晰.易于理解.由于借鉴了硬件总线的结构,而且各个插件之间是 ...
- 【转】Linux內核驅動之GPIO子系統(一)GPIO的使用 _蝸牛
原文网址:http://tc.chinawin.net/it/os/article-2512b.html 一 概述 Linux內核中gpio是最簡單,最常用的資源(和interrupt ,dma,ti ...
- 一台机器同时运行多个appium实例
测试需要同时在多个android设备上运行,就需要启动多个appium 第一台是运行微信: DesiredCapabilities capabilities = new DesiredCapabili ...
- php之手机号码查归属地
免费手机号码归属地API查询接口 一.淘宝网API API地址: http://tcc.taobao.com/cc/json/mobile_tel_segment.htm?tel=1585078144 ...
- hcharts
折线图 http://www.hcharts.cn/demo/index.php?p=10 饼状图 http://higrid.net/docs/highcharts_cn/#plotOptions- ...
- iOS 消息推送原理
一.消息推送原理: 在实现消息推送之前先提及几个于推送相关概念,如下图: 1. Provider:就是为指定IOS设备应用程序提供Push的服务器,(如果IOS设备的应用程序是客户端的话,那么Prov ...