首页 > 编程语言 > 详细

C语言按指定分隔符拆分字符串

时间:2021-06-21 20:19:46      阅读:14      评论:0      收藏:0      [点我收藏+]

 

C语言按指定分隔符拆分字符串

1. 先看下面的函数

参数1:分隔符、

参数2:字符串

参数3:分割后的字符串存放的位置

参数4:预计需要分割的个数

int at_get_words(char chop,char *srcStr, char **word, int size)
{
    int index = 0;
    int i = 0;
    char *str = srcStr;
    while (*(str + i) != \0)
    {
        if (*(str + i) == chop)
        {
            word[index] = str;
            word[index++][i] = \0;
            str = (str + i + 1);
            i = -1;
        }
        if (*(str + i) == \r)
        {
            word[index] = str;
            word[index++][i] = \0;
            str = (str + i);
            i = 0;
            break;
        }
        if (index >= size)
        {
            return index;
        }
        i++;
    }
    if (strlen(str) > 0)
    {
        word[index++] = str;
    }
    return index;
}

2. 使用方法

char *words[5] = { NULL };
char buf[64] = "115200, 8, 1, NONE, NFC";
if (at_get_words(,,buf,words,5 ) == 5)
{
    printf("out 0:%s",word[0]);
    printf("out 1:%s",word[1]);
    printf("out 2:%s",word[2]);
    printf("out 3:%s",word[3]);
    printf("out 4:%s",word[4]);
}

 

C语言按指定分隔符拆分字符串

原文:https://www.cnblogs.com/429512065qhq/p/14913843.html

(0)
(0)
   
举报
评论 一句话评论(0
关于我们 - 联系我们 - 留言反馈 - 联系我们:wmxa8@hotmail.com
© 2014 bubuko.com 版权所有
打开技术之扣,分享程序人生!