当前位置:网站首页>Recursion will make strtok more attractive
Recursion will make strtok more attractive
2022-06-27 01:23:00 【Embedded Linux,】
The string inversion problem issued a few days ago , Later, a new student used the recursive method to realize , Looked at the , It's really beautiful .
The previous question of string inversion
The code is as follows
#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 Is a string splitting function , But many people don't know how to use this function .
strtok Function interpretation
char *strtok(char s[], const char *delim)
Put the input string s Decompose into a set of strings .
delim Is a split string , Notice here is the string .
First call ,s Point to the string to be decomposed , Then call again to put s set NULL.
for example :
#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;
}
The header file
#include<string.h>
Detailed instructions for use
Start
strtok Find the string that needs to be split delim after , Will take this. delim Set to \0 , This can also be seen from the above example , After segmentation , We'll use printf The output will stop at the first split string position .
end
When the string finds the end , Function will return NULL, So we can use NULL To determine whether the function execution ends .
Be careful
This function will destroy the contents of the original string , After the first segmentation , Original string s It's the first string after the segmentation .
strtok Function source code
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 */
}Explain the code above
#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);// At the first call, the final output will be , When the last call is not equal to null, the first output is
}
}
int main() {
printf("%s\n",input);
reverse(input," ");
return 0;
}If recursive understanding is not very clear , It can be used for Let's output
#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;
}

边栏推荐
- 解决STC8G1K08程序不能运行的问题和端口配置
- buuctf-pwn write-ups (6)
- Kept to implement redis autofailover (redisha) 15
- About Random Numbers
- ArcGIS 镶嵌数据集切片丢失问题处理
- 自定义类加载器对类加密解密
- NLP:Transformer在NLP自然语言领域的简介(预训练技术)、NLP模型发展(ELmo/GPT/BERT/MT-DNN/XLNet/RoBERTa/ALBERT)、经典案例之详细攻略
- memcached基础4
- Buuctf PWN write UPS (6)
- 乔治·华盛顿大学 : Hanhan Zhou | PAC:多智能体强化学习中具有反事实预测的辅助价值因子分解
猜你喜欢

自定义类加载器对类加密解密

建模规范:环境设置

XSS notes (Part 2)

解决unable to create a folder to save the sketch: mkdir sketch

Esp32-solo development tutorial to solve config_ FREERTOS_ UNICORE problem
![Custom jsp[if, foreach, data, select] tag](/img/a2/fc75c182d572d86f4466323e31d6c3.png)
Custom jsp[if, foreach, data, select] tag

Break through the performance bottleneck of image recognition through rust language computing acceleration technology

3-wire SPI screen driving mode

通过Rust语言计算加速技术突破图片识别性能瓶颈

JSON parsing, esp32 easy access to time, temperature and weather
随机推荐
Keepalived 实现 Redis AutoFailover (RedisHA)13
Memcached foundation 4
Bs-gx-016 implementation of textbook management system based on SSM
Amazon ElastiCache 飞速搭建缓存服务集群,这才叫快
NLP:Transformer在NLP自然语言领域的简介(预训练技术)、NLP模型发展(ELmo/GPT/BERT/MT-DNN/XLNet/RoBERTa/ALBERT)、经典案例之详细攻略
解决u8glib只显示一行文字或者不显示的问题
疫情期间居家办公的总结体会 |社区征文
史上最难618,TCL夺得电视行业京东和天猫份额双第一
get_sequencer的用法总结
玩转OLED,U8g2动画,增长数字和随机三角形等
接口隔离原则
3线spi屏幕驱动方式
memcached基础6
Kept to implement redis autofailover (redisha) 14
Solve the problem that only one line of text is displayed or not displayed in u8glib
Kept to implement redis autofailover (redisha) 11
用代码生成流程图,Markdown的使用方法
IIS deploy static web site and FTP service
Visual introduction to Matplotlib and plotnine
Tsinghua & Zhiyuan | cogview2: faster and better text image generation model