用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) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...
随机推荐
- 关于offsetWidth innerWidth的使用
最近因为有使用到offsetWidth 和innerWidth,刚开始以为这两个属性在jq何js之中是可以通用的,谁知道在js中使用innerWidth时,发现如果对页面元素使用它时,发现出来的是un ...
- contentInset,contentsize和contentOffset区别
contentInset,contentsize和contentOffset区别 今天看别人用到了contentInset,觉得很迷糊,于是gogle了一下,搜到了一篇分析的很好的文章,转在这里, ...
- dos下修复硬盘损坏的文件
点击开始-->运行-->输入cmd,出现DOS状态对话框.在光标处输入有损坏文件的磁盘盘符后回车(如文件夹在D盘就输入D:然后回车),再输入“CHKDSK”,回车即可看到相关检测信息.“C ...
- NGINX和PHP之间的环境变量传递
昨天遇到的,想将IP访问转换成域名访问.则NGINX需要将相关的变量转换后传递给PHP. 网上有一系统的方法: 前面讲过该不该把信息写在服务器配置文件里?.通过php扩展hidef来define常量, ...
- Qt编程之实现属性窗口编辑器
类似于这种: 就是右下角这个框,有属性名字和对应的value编辑. 这个Widget是作为一个QDockWidget被添加到QMainWindow中的.QMainWindow必须要有centralWi ...
- 1645: [Usaco2007 Open]City Horizon 城市地平线
1645: [Usaco2007 Open]City Horizon 城市地平线 Time Limit: 5 Sec Memory Limit: 64 MBSubmit: 315 Solved: ...
- 用copy只能复制文件,用xcopy却说参数错误?
dos里面没有复制文件夹这一个命令,这是个复合的命令,是由新建文件夹和将原文件夹里的东西全部复制进去两步组成的.就像剪贴一样,是先建立文件,然后再删除源文件.这是一种复合型的操作. 复制文件夹: 1. ...
- hdu1796:容斥入门题
简单的容斥入门题.. 容斥基本的公式早就知道了,但是一直不会写. 下午看到艾神在群里说的“会枚举二进制数就会容斥”,后来发现还真是这样.. 然后直接贴代码了 #include <iostream ...
- 面试题32.从1到n整数中1出现的次数
题目:输入一个整数n,求从1到n这n个整数的十进制表示中1出现的次数.例如输入12,从 1到12这些整数中包含1的数字中1,10,11和12,1一共出现了5次 本题可以直接变量1到n的n个数然后分别计 ...
- chrome加载本地js
通过chrome的扩展功能,可以执行一些本地脚本. 1.如何添加扩展程序 具体做法是:chrome -> 设置 -> 扩展程序 -> 加载正在开发的扩展程序 图(1.1) 图(1.2 ...