C语言 strtok函数分解字符串为一组字符串

本文详细介绍了C语言中的strtok函数,用于将字符串分解为一系列子串,通过实际示例展示了如何使用strtok处理字符串'cpu_clus0_freqs'并打印各个部分。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

          C语言 strtok函数分解字符串为一组字符串

 

 

一、SYNOPSIS
       #include <string.h>

       char *strtok(char *str, const char *delim);

二、DESCRIPTION
       The strtok() function breaks a string into a sequence of zero or more nonempty tokens.  On the first call to strtok() the string to be
       parsed should be specified in str.  In each subsequent call that should parse the same string, str must be NUL

三、strtok函数功能是分解字符串为一组字符串。s为要分解的字符串,delim为分隔符字符(如果传入字符串,则传入的字符串中每个字符均为分割符)。首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。

四、测试

  1、strtok.c

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

void main()
{
    int i;
	char*freq_split;
	char cpu_clus0_freqs[]="/sys/devices/system/cpu/cpufreq/policy0/interactive/boostpulse";
	printf("\n c strtok function! cpu_clus0_freqs=%s\n",cpu_clus0_freqs);

    printf("\n************************************\n");
	freq_split = strtok(cpu_clus0_freqs,"/");
    while(freq_split)
	{
         printf("[%d]%s\n",i,freq_split);
		 freq_split=strtok(NULL,"/");
		 i++;
    }
	printf("\n************************************\n");
	printf("\n c strtok function! cpu_clus0_freqs=%s\n",cpu_clus0_freqs);
}

  2、运行结果

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值