当前位置:网站首页>字符串分割函数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;
}
运行结果:
边栏推荐
- [Headline] Written test questions - minimum stack
- 632. 最小区间
- What is the function of the JSP out.println() method?
- 图解LeetCode——1161. 最大层内元素和(难度:中等)
- 【加密周报】经济衰退在加息气氛中蔓延 美联储“放手一搏”?盘点上周加密市场发生的重大事件
- 双队列实现栈?双栈实现队列?
- 当奈飞的NFT忘记了Web2的业务安全
- Cyber-Physical System State Estimation and Sensor Attack Detection
- JSP request对象功能详解说明
- Cash Ⅱ LeetCode_518_ change
猜你喜欢
Keepalived 高可用的三种路由方案
IP核:FIFO
How to solve the error when mysql8 installs make
电机原理动图合集
Interview high-frequency test questions solution - stack push and pop sequence, effective parentheses, reverse Polish expression evaluation
Collection of NFT tools
How to reinstall Win11?One-click method to reinstall Win11
[Solution] Emqx startup under win10 reports Unable to load emulator DLL, node.db_role = EMQX_NODE__DB_ROLE = core
GetHashCode方法与=
When Netflix's NFTs Forget Web2 Business Security
随机推荐
如何发现新的潜力项目?工具推荐
不要用jOOQ串联字符串
Transient Stability Distributed Control of Power System with External Energy Storage
已知中序遍历数组和先序遍历数组,返回后序遗历数组
回顾历史5次经济衰退时期:这一次可能会有何不同?
测试点等同于测试用例吗
Unknown CMake command “add_action_files“
bgp 聚合 反射器 联邦实验
Axure tutorial - the new base (small white strongly recommended!!!)
How to reinstall Win11?One-click method to reinstall Win11
工业信息物理系统攻击检测增强模型
【无标题】
JSP out.write()方法具有什么功能呢?
Redis-消息发布订阅
链上治理为何如此重要,波卡Gov 2.0又会如何引领链上治理的发展?
Arduino Basic Syntax
An Enhanced Model for Attack Detection of Industrial Cyber-Physical Systems
Short video SEO search operation customer acquisition system function introduction
解析正则表达式的底层实现原理
How does JSP use request to get the real IP of the current visitor?