当前位置:网站首页>字符串分割函数strtok练习
字符串分割函数strtok练习
2022-08-02 00:06:00 【BSP初级小学僧】
编写一个程序,按照相反的单词顺序显示命令行参数,即,如果命令行参数
是see you later,程序的显示应为later you see
分析:本题可以使用字符串分割函数来做,因为各个单词之间需要使用空格' '来进行分隔,我们可以以此为切入点,将各个单词使用分割函数strtok分隔开来,将分隔之后的字符串一次存入另一个指针数组当中,然后将此指针数组逆序打印出来即可。
代码:
/*
3.编写一个程序,按照相反的单词顺序显示命令行参数,即,如果命令行参数
是see you later,程序的显示应为later you see
*/
#include <stdio.h>
#include <string.h>
int main()
{
char str[] = "hello world ni hao world";
puts(str);
char *p[100];
char *ret;
int i = 0;
ret = strtok(str, " ");
while (ret != '\0')
{
i++;
p[i] = ret;
ret = strtok(NULL, " ");
}
while (i >= 0)
{
printf("%s ", p[i]);
i--;
}
return 0;
}运行结果:

边栏推荐
- 众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
- 08-SDRAM: Summary
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- Multi-feature fusion face detection based on attention mechanism
- [头条]笔试题——最小栈
- 微软电脑管家V2.1公测版正式发布
- 重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
- 测试点等同于测试用例吗
- Don't know about SynchronousQueue?So ArrayBlockingQueue and LinkedBlockingQueue don't and don't know?
- bgp 聚合 反射器 联邦实验
猜你喜欢

利用“栈”快速计算——逆波兰表达式

双队列实现栈?双栈实现队列?

什么是低代码(Low-Code)?低代码适用于哪些场景?

TCL: Pin Constraints Using the tcl Scripting Language in Quartus

security CSRF Vulnerability Protection

Study Notes: The Return of Machine Learning
![[21-Day Learning Challenge] A small summary of sequential search and binary search](/img/81/7339a33de3b9e3aec0474a15825a53.png)
[21-Day Learning Challenge] A small summary of sequential search and binary search

PHP从txt文件中读取数据的方法

Day11 shell脚本基础知识

How to find new potential projects?Tools recommended
随机推荐
How does JSP use the page command to make the JSP file support Chinese encoding?
面试高频考题解法——栈的压入弹出序列、有效的括号、逆波兰表达式求值
短视频SEO优化教程 自媒体SEO优化技巧方法
不就是个TCC分布式事务,有那么难吗?
JSP request对象功能详解说明
Is TCP reliable?Why?
ROS dynamic parameters
BGP 第一次实验
08-SDRAM: Summary
JSP内置对象out对象的功能简介说明
【21天学习挑战赛】顺序查找和二分查找的小总结
JSP out.write()方法具有什么功能呢?
NFT工具合集
07-SDRAM: FIFO control module
什么是低代码(Low-Code)?低代码适用于哪些场景?
Unity—四元数、欧拉角API+坐标系统
460. LFU 缓存
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
security CSRF Vulnerability Protection
C language Qixi is coming!It's time to show the romance of programmers!