当前位置:网站首页>PAT 乙等 1009 C语言
PAT 乙等 1009 C语言
2022-06-23 04:11:00 【章鱼bro】
1009. 说反话 (20)
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:Hello World Here I Come输出样例:
Come I Here World Hello
思路:将输入的句子从后往前寻找空格,此处利用strtchr函数,此函数是从右向左寻找某个字符,找到返回该字符的指针,找不到返回NULL。找到空格后将空格赋值为‘\0’,相当于将最后一个单词和前面的句子隔开。再将指针加一,指向被截出来的单词的第一个字母,再输出从该指针位置开始的字符串,即输出的是最后一个单词。
一、起始变量
1.字符串array。
2.接受空格地址的指针
二、运算
1.接受字符串
2.从字符串结尾开始向前寻找空格
3.找到空格将此位置‘\0’,并将指针加一
4.输出从指针位置开的字符串
三、代码
#include "stdio.h"
#include "string.h"
int main()
{
char array[81];
gets(array);//1.接受字符串
char * p;
int i;
for(i = strlen(array) - 1; i >= 0; i--,i = strlen(array) - 1)
{
p = strrchr(array,' ');//2.从字符串结尾开始向前寻找空格,若找不到空格则返回NULL
if(!p)
{
printf("%s",array);
break;
}//如果p为空,说明该字符串已经遍历完
*p = '\0';//3.找到空格将此位置‘\0’,并将指针加一
p++;
printf("%s ",p);//4.输出从指针位置开的字符串
}
return 0;
},
边栏推荐
- PAT 乙等 1024 科学记数法 C语言
- Win11应用商店一直转圈解决办法
- Win11 app store keeps turning around solution
- Redis cache penetration solution - bloom filter
- MySQL面试真题(二十九)——案例-找到爱看的电影
- Ansible 使用普通用户管理被控端
- PAT 乙等 1026 程序运行时间
- Low cost 5W wireless charger scheme fs68001b simple charging chip
- 51万奖池邀你参战——第二届阿里云ECS CloudBuild开发者大赛来袭
- 使用链表实现两个多项式相加和相乘
猜你喜欢

Wechat applet: a new interesting test

Yingjixin ip6806 wireless charging scheme 5W Qi certified peripheral simplified 14 devices

Composite API

Real MySQL interview question (30) -- shell real estate order analysis

C primer plus學習筆記 —— 2、常量與格式化IO(輸入/輸出)

FS2119A同步升压IC输出3.3V和FS2119B同步升压IC输出5V

知识点滴 - 折叠锻打和大马士革钢

Opportunities and challenges of digital collections from the perspective of technology development team

数字化工厂建设可划分为三个方面

Win11应用商店下载的软件怎么移到桌面
随机推荐
Alibaba cloud object storage oss+picgo+typera implements the construction map
Advanced Mathematics (Seventh Edition) Tongji University exercises 1-9 personal solutions
Jvm: when a method is overloaded, the specific method to call is determined by the static type of the incoming parameter rather than the actual type of the parameter
Raspberry pie assert preliminary exercise
MySQL Foundation
FS4059A与FS5080E充电芯片的区别
B-string value (string DP) of the 16th Northeast College Students' Programming Competition (warm-up)
PAT 乙等 1020.月饼
Wechat applet: elderly blessing short video
MySQL面试真题(二十六)——滴滴2020年笔试题
Management system of borrowed books based on SSM framework
Common wireless charging and transmitting IC chips
[proteus simulation] Arduino uno+pcf8574+lcd1602+mpx4250 electronic scale
True question of MySQL interview (29) -- case - finding favorite movies
MySQL面试真题(二十九)——案例-找到爱看的电影
知识点滴 - 折叠锻打和大马士革钢
The author believes that the so-called industrial Internet is a process of deep integration of industry and the Internet
AI艺术的基因工程?使用 #Artbreeder 改变图像的任意形态
CF [1700d] D. River locks (DP, bisection, Mathematics)
Leetcode 797: all possible paths