将字符串拆分为标记并将它们保存在数组中 [英] Split string into tokens and save them in an array

查看:39
本文介绍了将字符串拆分为标记并将它们保存在数组中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如何将一个字符串拆分成一个标记,然后将它们保存在一个数组中?

How to split a string into an tokens and then save them in an array?

具体来说,我有一个字符串"abc/qwe/jkh".我想把"/"分开,然后把token保存到一个数组中.

Specifically, I have a string "abc/qwe/jkh". I want to separate "/", and then save the tokens into an array.

输出是这样的

array[0] = "abc"
array[1] = "qwe"
array[2] = "jkh"

请帮帮我

推荐答案

#include <stdio.h>
#include <string.h>

int main ()
{
    char buf[] ="abc/qwe/ccd";
    int i = 0;
    char *p = strtok (buf, "/");
    char *array[3];

    while (p != NULL)
    {
        array[i++] = p;
        p = strtok (NULL, "/");
    }

    for (i = 0; i < 3; ++i) 
        printf("%s\n", array[i]);

    return 0;
}

这篇关于将字符串拆分为标记并将它们保存在数组中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆