当前位置:网站首页>字符串分割函数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;
}
运行结果:
边栏推荐
猜你喜欢
bgp 聚合 反射器 联邦实验
Collection of NFT tools
Disk and file system management
security cross-domain configuration
632. 最小区间
不了解SynchronousQueue?那ArrayBlockingQueue和LinkedBlockingQueue不会也不知道吧?
Double queue implementation stack?Dual stack implementation queue?
Keepalived 高可用的三种路由方案
Axure tutorial - the new base (small white strongly recommended!!!)
不就是个TCC分布式事务,有那么难吗?
随机推荐
单片机遥控开关系统设计(结构原理、电路、程序)
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
08-SDRAM: Summary
nodeJs--各种路径
【21天学习挑战赛】顺序查找和二分查找的小总结
Industrial control network intrusion detection based on automatic optimization of hyperparameters
What is it like to trade for a living?
[头条]笔试题——最小栈
Arduino Basic Syntax
在不完全恢复、控制文件被创建或还原后,必须使用 RESETLOGS 打开数据库,解释 RESETLOGS.
重装腾讯云云监控后如果对应服务不存在可通过sc.exe命令添加服务
Short video SEO search operation customer acquisition system function introduction
路由策略
Day11 shell脚本基础知识
扑克牌问题
How to solve the error when mysql8 installs make
CRS 管理与维护
C language Qixi is coming!It's time to show the romance of programmers!
Task execution control in Ansible
Async/await principle and execution sequence analysis