#include <iostream>
#include <stdio.h>
#include <string.h>
const int SIZE = 1024 * 8;
char APPBUF[SIZE]; // 设置缓冲区 APPBUF
// 本函数完成从 分词里找最长的分词,使用缓冲区,避免了内存泄漏
char *search(const char* chbuf , const char* val);

int main()
{
    const char *str = "我/爱/中珅/||我/爱珅/中文/||我爱1234567890/中/文/||我爱/中文/分/||我爱/中文/分词/";
    char *pch = search(str , "|");
    printf("%s\n", pch);
    printf("%s\n", search("http://topic.csdn.net/u/20110822/20/e369cc10-5392-4763-b2cd-da4995b2cafb.html" , "/"));
    printf("%s.png\n", search("http://hi.csdn.net/attachment/201108/22/3362235_1313993745S1YP.png" , "/."));
    return 0;
}

char *search(const char* chbuf , const char* val)
{
    char *ret = APPBUF;  // 使用缓冲区,先清0
    memset(ret, 0, SIZE);
    char *pch = NULL;
    char *funbuf = new char[strlen(chbuf)+1];
    strcpy(funbuf, chbuf);

pch = strtok(funbuf, val);
    while (pch != NULL) {
        if (strlen(ret) < strlen(pch)) { // 获取最长的字符串单元
            strcpy(ret, pch);
        }
        pch = strtok(NULL, val);
    }
    delete[] funbuf;
    return ret;
}

#if 0  // 以下为代码资料,不编译

char *strtok(char *str1, const char *str2);
功能:函数返回字符串str1中紧接“标记”的部分的指针, 字符串str2是作为标记的分隔符。
如果分隔标记没有找到,函数返回NULL。为了将字符串转换成标记,
第一次调用str1 指向作为标记的分隔符。之后所以的调用str1 都应为NULL。

char *strtok (char * string, const char * control)
{
    unsigned char *str;
    const unsigned char *ctrl = control;

unsigned char map[32];
    int count;

static char *nextoken;

/* Clear control map */
    for (count = 0; count < 32; count++)
        map[count] = 0;

/* Set bits in delimiter table */
    do {
        map[*ctrl >> 3] |= (1 << (*ctrl & 7));
    } while (*ctrl++);

/* Initialize str. If string is NULL, set str to the saved
     * pointer (i.e., continue breaking tokens out of the string
     * from the last strtok call) */
    if (string)
        str = string;
    else
        str = nextoken;

/* Find beginning of token (skip over leading delimiters). Note that
     * there is no token iff this loop sets str to point to the terminal
     * null (*str == '\0') */
    while ( (map[*str >> 3] & (1 << (*str & 7))) && *str )
        str++;

string = str;

/* Find the end of the token. If it is not the end of the string,
     * put a null there. */
    for ( ; *str ; str++ )
        if ( map[*str >> 3] & (1 << (*str & 7)) ) {
            *str++ = '\0';
            break;
        }

nextoken = str;

/* Determine if a token has been found. */
    if ( string == str )
        return NULL;
    else
        return string;
}

#endif

strtok是分割字符串,查找中间最长的单元相关推荐

  1. C++中使用strtok函数分割字符串String

    C++中使用strtok函数分割字符串String string str; getline(cin,str); vector<string> vec; char *p = strtok(( ...

  2. C/C++根据特定字符分割字符串、读取文件去掉逗号等特定字符、strtok()函数详解

    字符串分割情况 读取文件时,C++识别的是空格和换行符,但有时候文件是以符号分割的,如逗号等 字符串本身含有特殊符号,如逗号,@等 strtok()函数 strtok()函数能够按照特定的字符分解字符 ...

  3. 【摘录】C语言中利用 strtok函数进行字符串分割

    C语言不像Java,Php之类的高级语言,对象中直接封装了字符串的处理函数.C语言中进行普通的字符串处理也经常会让我们焦头烂额--不过好在C语言 中还是提供了像strtok这样功能强大的字符串处理函数 ...

  4. C分割字符串以及strtok

    C分割字符串以及strtok 定义: int main() {char array[] = { "192.168.1.222" }; //要分割的字符串char buf[5][32 ...

  5. Java对于字符串的处理【String和int之间的转换、字符串拼接、字符串获取长度、字符串大小写转换、字符串去空格、字符串分割、字符串替换、字符串提取、字符串比较、字符串查找】

    文章目录 String字符串 和 整型int 的相互转换 String转化为int int 转换为 String 字符串拼接 使用连接运算符 "+" 使用 concat() 方法 ...

  6. B00009 C语言分割字符串库函数strtok

    切割字符串是常用的处理. 这里给出一个使用函数strtok切割字符串的例子. 使用C语言的库函数strtok来切割字符串的好处在于,可以指定任意字符作为分隔符来切割单词.使用该函数,切割字符串的分隔符 ...

  7. 【C语言】字符串函数strtok 按照指定字符串分割

    C语言字符串函数 strtok() 函数原型 char *strtok(char *str,const char *delimiters); 参数 str,待分割的字符串 delimiters,分隔符 ...

  8. C语言两个字符串查找最长的公共子串的算法(附完整源码)

    C语言两个字符串查找最长的公共子串的算法 C语言两个字符串查找最长的公共子串的算法完整源码(定义,实现,main函数测试) C语言两个字符串查找最长的公共子串的算法完整源码(定义,实现,main函数测 ...

  9. Java 字符串常用操作(比较、查找位置、删除字符、替换字符串、反向输出、是否存在、分割字符串、大小写转换,区域比较、拼接字符串)

    字符串比较 public class StringCompareEmp{public static void main(String args[]){String str = "Hello ...

最新文章

  1. 生产Docker应用重启排查经历
  2. 麦迪逊大学计算机科学咋样,威斯康星大学麦迪逊分校计算机科学基本信息全览...
  3. Mysql之数据库锁(表锁和行锁)详解
  4. mysql关键字test_MySQL关键字Distinct的详细介绍
  5. java匿名类_Java匿名类
  6. Flash CS4从入门到精通
  7. Pycharm中.py文件调用其他.py文件的函数
  8. 高德地图商户标注平台上线 免费标注线下店铺
  9. 第二十九章 管理许可(二)
  10. ffmpeg 图片序列转视频
  11. web开发浏览器缓存问题之Google浏览器缓存清理
  12. 表格table标签的属性及使用方式
  13. php nette,thinkphp 采用nettemail发送邮件
  14. 线性代数:矩阵的LU分解
  15. 为什么高级Android程序员永远不必担心自己的技术过时?
  16. 德勤全球智慧城市2.0报告发布!
  17. 遍历Map集合的两种方法
  18. WAF网页应用防火墙详解(设计思路-防御恶意文件上传实例-厂商防御技术-Imperva WAF部分特色功能了解)
  19. 各类多项式操作的暴力递推法
  20. SVPWM仿真和基于DSP28335的PIL(处理器在环) 仿真模型(将matlab仿真算法生成代码在DSP中在线运行返回数据给Matlab)验证算法可行性和实时性

热门文章

  1. 高性能Web服务器:Nginx
  2. NCH WavePad Mater for Mac(音乐编辑器)
  3. Pagerduty - prometheus - grafana测试告警的使用方法
  4. wdcp-apache配置错误导致进程淤积进而内存吃紧
  5. 5-2 人际资源整合-正确看待自己的领导-与领导日常互动-说服领导的技巧
  6. springboot在项目启动时加载字典表数据进map内存,真实测试通过。
  7. Android设置壁纸的几种方案
  8. PowerBuilder篇(2)——如何连接Oracle数据库
  9. setVisibility
  10. matlab+中文字体设计,有搞头没有? 有搞头