用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) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
随机推荐
- IE6双倍margin间距解决方法
虽然现在很少有人在用ie6了,但作为前端人员,是不的不面对的问题. 在ie6中,使用float后的元素,在margin—left,这是你会发现距离和你想的不一样,其他的浏览器又是正常的,增大margi ...
- linux内核学习之四:进程切换简述
在讲述专业知识前,先讲讲我学习linux内核使用的入门书籍:<深入理解linux内核>第三版(英文原版叫<Understanding the Linux Kernel>),不过 ...
- 安卓 eclipse项目创建
一. 创建项目工程 1. 点击 file -> new -> Android Application project -> 2. 创建工程项目名字 自己命名 (注: 不要出现 ...
- js限制input只能输入有效的数字,有且只有一个小数点,第一个不能为小数点-备
<!doctype html> <html lang="en"> <head> <meta charset="UTF-8&quo ...
- Oracle set autotrace 时提示:Cannot find the Session Identifier. Check PLUSTRACE role is enabled
SQL> set autotrace Usage: SET AUTOT[RACE] {OFF | ON | TRACE[ONLY]} [EXP[LAIN]] [STAT[ISTICS]] SQL ...
- 这样就算会了PHP么?-1
公司有用到这些业务,多了解一下总是没错的. 现在开始一系列的PHP学习啦... <?php $boo=true; if($boo==true) echo '变量$boo为真!'; else ec ...
- JavsScript的基本特点
1.简单性Javascript是一种脚本语言,它采用小程序段的方式实现编程它同样也是一种解释性语言.2.动态性Javascript是动态的,它可以直接对用户或者客户输入做出相应,无须经过Web服务程序 ...
- javascript学习笔记——chrome等提示找不到“getElementsByTagName”的一种解决方法
最近学习是写了一个小网页,前台有个下拉框是通过后天的xml配置的,在写好代码后使用发现在IE9以及之前的IE浏览器都可以正常获取,但是IE10,chrome和firefox都会在获取一个标签时报get ...
- 3D打印论坛
3D打印论坛:http://www.3done.cn http://www.03dp.com www.qjxxw.net/ http://www.3ddayin.net http://oa.zol.c ...
- [置顶] android之存储篇_SQLite数据库_让你彻底学会SQLite的使用
SQLite最大的特点是你可以把各种类型的数据保存到任何字段中,而不用关心字段声明的数据类型是什么. 例如:可以在Integer类型的字段中存放字符串,或者在布尔型字段中存放浮点数,或者在字符型字段中 ...