#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实现字符串分割并返回所有子串的更多相关文章

  1. MSSQLSERVER数据库- 字符串分割函数返回类型表

    遇到这样一个问题,存储在数据库的数据是一串字符串如:1,2,3,4,5,6.想把这串字符串进行转变成一个表格,如下: 1 2 3 4 5 6 就是这样一个问题,有人同事,写了一个这样的封装函数,这样就 ...

  2. [leetcode]131. Palindrome Partitioning字符串分割成回文子串

    Given a string s, partition s such that every substring of the partition is a palindrome. Return all ...

  3. Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割

    ylbtech-Java-Runoob-高级教程-实例-字符串:07. Java 实例 - 字符串分割 1.返回顶部 1. Java 实例 - 字符串分割  Java 实例 以下实例使用了 split ...

  4. Lua 用指定字符或字符串分割输入字符串,返回包含分割结果的数组

    // 用指定字符或字符串分割输入字符串,返回包含分割结果的数组 // @function [parent=#string] split // @param string input 输入字符串 // ...

  5. JS对象 字符串分割 split() 方法将字符串分割为字符串数组,并返回此数组。 语法: stringObject.split(separator,limit)

    字符串分割split() 知识讲解: split() 方法将字符串分割为字符串数组,并返回此数组. 语法: stringObject.split(separator,limit) 参数说明: 注意:如 ...

  6. SQL Server 游标运用:鼠标轨迹字符串分割

    一.本文所涉及的内容(Contents) 本文所涉及的内容(Contents) 背景(Contexts) 游标模板(Cursor Template) 鼠标轨迹字符串分割SQL脚本实现(SQL Code ...

  7. Oracle 超长字符串分割劈分

    Oracle 超长字符串分割劈分,具体能有多长没测过,反正很大.... 下面,,,,直奔主题了: CREATE OR REPLACE FUNCTION splitstr(p_string IN clo ...

  8. Python 字符串分割的方法

    在平时工作的时候,发现对于字符串分割的方法用的比较多,下面对分割字符串方法进行总结一下:第一种:split()函数split()函数应该说是分割字符串使用最多的函数用法:str.split('分割符' ...

  9. 字串符相关 split() 字串符分隔 substring() 提取字符串 substr()提取指定数目的字符 parseInt() 函数可解析一个字符串,并返回一个整数。

    split() 方法将字符串分割为字符串数组,并返回此数组. stringObject.split(separator,limit) 我们将按照不同的方式来分割字符串: 使用指定符号分割字符串,代码如 ...

随机推荐

  1. js 发送ajax请求(XMLHttpRequest)

    <!DOCTYPE html><html> <head> <title></title> <script type="tex ...

  2. android技术下载

    http://www.linuxidc.com/Linux/2011-08/41339.htm http://linux.linuxidc.com/2011%E5%B9%B4%E8%B5%84%E6% ...

  3. linux下如何编译python生成libpython2.5.so动态库

    http://biancheng.dnbcw.info/python/245306.html

  4. C#中使用命名管道进行进程通信的实例

    原文:C#中使用命名管道进行进程通信的实例 1 新建解决方案NamedPipeExample 在解决方案下面新建两个项目:Client和Server,两者的输出类型均为"Windows 应用 ...

  5. PCIE体系结构

    http://blog.sina.com.cn/s/articlelist_1685243084_3_1.html BAR寄存器 http://zhidao.baidu.com/link?url=rE ...

  6. linux文件合并

    第一:两个文件的交集,并集前提条件:每个文件中不得有重复行1. 取出两个文件的并集(重复的行只保留一份)2. 取出两个文件的交集(只留下同时存在于两个文件中的文件)3. 删除交集,留下其他的行1. c ...

  7. ASP.NET MVC4中的Model验证 移除指定验证信息

    MVC中通过Model在页面间传值使的程序开发变得更加的快捷,但是很多时候,我们在数据传递的时候为了确保数据的有效性,要对Model的相关属性做基本的数据验证. 不多说直接上个代码,Model的实体类 ...

  8. Spark集群模式概述

    作者:foreyou出处:http://www.foreyou.net/2015/06/22/spark-cluster-mode-overview/声明:本文采用以下协议进行授权: 署名-非商用|C ...

  9. cf437C The Child and Toy

    C. The Child and Toy time limit per test 1 second memory limit per test 256 megabytes input standard ...

  10. Lucene 高亮功能

    原文转载自: http://qindongliang1922.iteye.com/blog/1953409 高亮功能一直都是全文检索的一项非常优秀的模块,在一个标准的搜索引擎中,高亮的返回命中结果,几 ...