当前位置:网站首页>字符串分割函数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;
}运行结果:

边栏推荐
- ROS 动态参数
- 解析正则表达式的底层实现原理
- 【无标题】
- Disk and file system management
- 单片机遥控开关系统设计(结构原理、电路、程序)
- Axure tutorial - the new base (small white strongly recommended!!!)
- Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
- JSP page指令errorPage属性起什么作用呢?
- [头条]笔试题——最小栈
- Short video SEO optimization tutorial Self-media SEO optimization skills and methods
猜你喜欢
随机推荐
A simple file transfer tools
Collection of NFT tools
一篇永久摆脱Mysql时区错误问题,idea数据库可视化插件配置
Keepalived 高可用的三种路由方案
IO流基础
2022/08/01 学习笔记 (day21) 泛型和枚举
众筹DAO“枯萎”的缩影:曾拍下《沙丘》未出版手稿的Spice DAO解散
[Headline] Written test questions - minimum stack
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
不就是个TCC分布式事务,有那么难吗?
OpenCV DNN blogFromImage() detailed explanation
How to find new potential projects?Tools recommended
认识USB、Type-C、闪电、雷电接口
Simpson's paradox
460. LFU cache
玩转NFT夏季:这份工具宝典值得收藏
els block boundary deformation processing
TCL:在Quartus中使用tcl脚本语言进行管脚约束
PHP从txt文件中读取数据的方法
Unknown CMake command “add_action_files“









