#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. 怪胎:Android开发ImageView图片无法显示

    今天碰到一个非常奇怪的问题: 在Android中ImageView无法显示加载的本地SDCard图片. 具体过程是:先调用本地照相机程序摄像,然后将拍摄的图片加载在ImageView中显示. publ ...

  2. QDialog 模态对话框与事件循环(exec其实就是调用了show和eventLoop.exec)

    起源 qtcn中文论坛中有网友问到: 假设程序正常运行时,只有一个简单的窗体A,此时只有一个GUI主线程,在这个主线程中有一个事件循环处理窗体上的事件.当此程序运行到某阶段时,弹出一个模态窗体B(书上 ...

  3. 单例模式 - OK

    单例模式(Singleton):保证一个类仅有一个实例,并提供一个访问它的全局访问点. 一.单例模式 通常我们可以让一个全局变量使得一个对象被访问,但它不能防止你实例化多个对象.一个最好的办法就是,让 ...

  4. bzoj1681[Usaco2005 Mar]Checking an Alibi 不在场的证明

    Description A crime has been comitted: a load of grain has been taken from the barn by one of FJ's c ...

  5. CSDN第四届在线编程大赛2014初赛:带通配符的数

    题目要求: 输入参数:参数A,含有任意个数的?的数值字符串,如:12?4,?代表一位任意数             参数B,不含?的数值字符串,长度与参数A一致输出结果:参数A比参数B大的可能数值个数 ...

  6. poj1881:素因子分解+素数测试

    很好的入门题 先测试是否为素数,若不是则进行素因子分解,算法详见总结贴 miller robin 和pollard rho算法 AC代码 #include <iostream> #incl ...

  7. Ubuntu mysql安装,还有可视化界面

    安装mysql sudo apt-get install mysql-server sudo apt-get install mysql-client sudo apt-get install lib ...

  8. 第27讲 UI组件之 ScrollView与底部动态添加数据

    第27讲 UI组件之 ScrollView与底部动态添加数据 1. ScrollView(滚动视图) ScrollView(滚动视图)是实现滚动的一个控件,只需要将需要滚动的控件添加到ScrollVi ...

  9. JAVA并发实现四(守护线程和线程阻塞)

    守护线程     Java中有两类线程:User Thread(用户线程).Daemon Thread(守护线程) 用户线程即运行在前台的线程,而守护线程是运行在后台的线程. 守护线程作用是为其他前台 ...

  10. mysql 获取当前时间戳

      mysql 获取当前时间为select now() 运行结果: 2012-09-05 17:24:15 mysql 获取当前时间戳为select unix_timestamp(now()) 运行结 ...