当前位置:网站首页>递归是会更秀strtok
递归是会更秀strtok
2022-06-27 00:49:00 【嵌入式Linux,】
前几天发的字符串反转题目,后面有一个新同学用了递归的方法来实现,看了下,真的是很秀。
代码如下
#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};
void reverse(char *s,char *delim) {
char* temp = strtok(s,delim);
if(temp != NULL) {
reverse(NULL,delim);
printf("%s ",temp);
}
}
int main() {
printf("%s\n",input);
reverse(input," ");
return 0;
}strtok是一个字符串分割函数,但是这个函数估计很多人还不明白他的用法。
strtok函数解释
char *strtok(char s[], const char *delim)
把输入的字符串s分解成为一组字符串。
delim 为分割字符串,注意这里是字符串。
首次调用时,s指向要分解的字符串,之后再次调用要把s设成NULL。
例如:
#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};
int main() {
char *temp = NULL;
printf("%s\n",input);
temp = strtok(input," ");
printf("input=%s,temp=%s\n", input, temp);
temp = strtok(NULL," ");
printf("input=%s,temp=%s\n", input, temp);
return 0;
}
头文件
#include<string.h>
使用详细说明
开始
strtok 找到需要分割的字符串delim后,会把这个delim设置成\0 ,这从上面的实例也可以看出来,分割之后,我们再用printf输出会在第一个分割字符串位置停止输出。
结束
当字符串查找到末尾时,函数会返回NULL,所以我们可以用NULL来判断函数执行是否结束。
注意
用这个函数处理之后会破坏原来字符串中的内容,第一次分割之后,原字符串s是分割完成之后的第一个字符串。
strtok函数源码
char *
strtok_apple(
register char *s,
register const char *delim)
{
register char *spanp;
register int c, sc;
char *tok;
static char *last;
if (s == NULL && (s = last) == NULL)
return (NULL);
/*
* Skip (span) leading delimiters (s += strspn(s, delim), sort of).
*/
cont:
c = *s++;
for (spanp = (char *)delim; (sc = *spanp++) != 0;) {
if (c == sc)
goto cont;
}
if (c == 0) { /* no non-delimiter characters */
last = NULL;
return (NULL);
}
tok = s - 1;
/*
* Scan token (scan for delimiters: s += strcspn(s, delim), sort of).
* Note that delim must have one NUL; we stop if we see that, too.
*/
for (;;) {
c = *s++;
spanp = (char *)delim;
do {
if ((sc = *spanp++) == c) {
if (c == 0)
s = NULL;
else
s[-1] = 0;
last = s;
return (tok);
}
} while (sc != 0);
}
/* NOTREACHED */
}解释下上面的代码
#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};
void reverse(char *s,char *delim) {
char* temp = strtok(s,delim);
if(temp != NULL) {
reverse(NULL,delim);
printf("%s ",temp);//第一次调用的时候会最后输出,最后一次调用不等于空的时候最先输出
}
}
int main() {
printf("%s\n",input);
reverse(input," ");
return 0;
}如果递归理解不是很明白,可以用for来输出看看
#include "stdio.h"
#include "string.h"
char input[] = {"the sky is blue cris 1212321 apple"};
int main() {
char * output[1024];
int len = 0;
printf("%s\n",input);
char* temp = strtok(input," ");
for (len=0;temp!=NULL;temp = strtok(NULL," "),++len) {
output[len] = temp;
}
for(;len--;) {
printf("%s ", output[len]);
}
return 0;
}

边栏推荐
- Esp32-solo development tutorial to solve config_ FREERTOS_ UNICORE problem
- Keepalived 实现 Redis AutoFailover (RedisHA)12
- leetcode 1143. Longest Commom Subsequence 最长公共子序列(中等)
- Tsinghua & Zhiyuan | cogview2: faster and better text image generation model
- BS-GX-016基于SSM实现教材管理系统
- At present, which securities company is the best and safest to open an account for stock speculation?
- 持续交付-Blue Ocean 应用
- 3線spi屏幕驅動方式
- 解决unable to create a folder to save the sketch: mkdir sketch
- Great vernacular with high concurrency (I)
猜你喜欢

使用NetworkX对社交网络进行系统的分析:Facebook网络分析案例

ML:机器学习工程化之团队十大角色背景、职责、产出物划分之详细攻略

The world is very big. Some people tattoo QR codes on their necks

建模规范:环境设置

Review the old and know the new -- constant renewal at normal temperature

Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk

Ml: a detailed introduction to the division of the top ten roles, backgrounds, responsibilities and outputs of the machine learning engineering team

JSON parsing, esp32 easy access to time, temperature and weather

IIS deploy static web site and FTP service

做了两天的唯美蝴蝶动画
随机推荐
Tsinghua & Zhiyuan | cogview2: faster and better text image generation model
UVM中uvm_report_enabled的用法
CH423要如何使用,便宜的国产IO扩展芯片
Solution of idea hot start failure
Summary of working at home during the epidemic | community essay solicitation
Beyond lithium battery -- the concept of battery in the future
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
XSS攻击笔记(上)
Find the minimum value in the rotation sort array ii[classical Abstract dichotomy + how to break the game left, middle and right are equal]
Operating instructions and Q & A of cec-i China learning machine
memcached基础
IIS 部署静态网站和 FTP 服务
Topolvm: kubernetes local persistence scheme based on LVM, capacity aware, dynamically create PV, and easily use local disk
uvm中的config机制方法总结(一)
一键加速索尼相机SD卡文件的复制操作,文件操作批处理教程
Kept to implement redis autofailover (redisha) 17
Central Limit Theorem
Custom class loader encrypts and decrypts classes
Structure the fifth operation of the actual camp module
Implementation of ARP module in LwIP