【无标题】C语言字符串分割

c代码实现字符串分割 (通用于字节分割)

在公司使用过的
输入
nod : 开始分割的起始位置
source_str:代分割的字符串地址
dest_str : 保存分割的数组地址
dest_str_len:保存分割的数组长度
ch:分割的字符
return:分割以后新的起始位置

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <stdio.h>
#include <string.h>
static int split_str(int nod, uint8_t *source_str, uint8_t *dest_str, int dest_str_len, uint8_t ch)
{
    if (NULL == source_str || NULL == dest_str)
    {
        return -1;
    }
    int len;

    len = strlen(source_str);
    int i;
    for (i = nod; i < len; i++)
    {
        if (*(source_str + i) == ch)
        {
            break;
        }
    }
    memcpy(dest_str, source_str + nod, i - nod);

    if ((i - nod) < dest_str_len)
    {
        dest_str[i - nod] = '\0';
    }

    if (i == len)
        return 0;
    else
        return (++i);
}
int main()
{
    char str_s[100] = "$GNGGA,, 024817.00,3109.95680723,N,12117.28029833,E,1,28,0.6,44.9440,M,0.0000,M,,*7D,,,";
    int nod = 0;
    char str_d[100] = {0};
    int i;
    for (i = 0; i < 20; i++)
    {
        nod = split_str(nod, str_s, str_d, sizeof(str_d), ',');
        printf("%s\n", str_d);
    }
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

qq_30789055

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值