#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. Linux下关闭node应用

    今天在折腾用node接入微信公众号时,碰到了node应用启动后卡死退出,需要找出该进程关闭的问题,由于对shell脚本不是很熟悉,记录如下: 我们在用npm start启动应用后,通常要关闭时,ctr ...

  2. HTTP缓存机制梳理

    一般只对GET类型请求做缓存,对POST不做缓存 缓存协商 指浏览器和Web服务器之间对于是否使用浏览器端缓存的协商.对于较小的文件内容,由于缓存协商仍需要发送请求,所以吞吐率提高不大:但没有正文传输 ...

  3. iOS设备后台播放音乐方法

    iOS设备后台播放音乐方法 1 在设置Capabliites中打开Background Modes,选择Audio And AirPlay 2 在控制viewDidLoad中添加下面代码 AVAudi ...

  4. leetcode_Search in Rotated Sorted Array II

    Follow up for "Search in Rotated Sorted Array": What if duplicates are allowed? Would this ...

  5. url中的jsessionid解释

    (1) 这是一个保险措施 因为Session默认是需要Cookie支持的 但有些客户浏览器是关闭Cookie的 这个时候就需要在URL中指定服务器上的session标识,也就是5F4771183629 ...

  6. jquery 的ready() 与window.onload()的区别

    做web开发时常用Jquery中$(document).ready()和JavaScript中的window.onload方法,两者都是要在页面加载完成以后加载的方法,但是这两者还是有很大区别的.最近 ...

  7. 利用Visual Studio寻找C#程序必要的运行库文件

    在工程打包中,有时候很头痛的就是运行所需要的库文件不能够全面的包含进来,特别是有时候调用了一系列外部扩展.对于这些问题,我们可以借用Visual Studio的打包功能帮助我们寻找软件运行必须的库文件 ...

  8. 如何让程序(如java Hello)只启动一次?

    如何让程序(如java Hello)只启动一次? 摘自http://bbs.csdn.net/topics/50488704 总结一下,关于让Java程序只运行一个实例的问题,其实质是JVM之间通信的 ...

  9. git hub 资料汇总

    tobecrazy  Selenium automation test framework    https://github.com/tobecrazy/Demo Smartphone Test F ...

  10. JavaScript闭包函数的写法

    <script type="text/javascript"> //通过js内置的函数构造器创建函数 var func=new Function('a','b','re ...