PAT (Basic Level) Practise (中文)1009. 说反话 (20)
来源:互联网 发布:sql 查询某个字段包含 编辑:程序博客网 时间:2024/05/17 08:30
1009. 说反话 (20)
时间限制
400 ms
内存限制
65536 kB
代码长度限制
8000 B
判题程序
Standard
作者
CHEN, Yue
给定一句英语,要求你编写程序,将句中所有单词的顺序颠倒输出。
输入格式:测试输入包含一个测试用例,在一行内给出总长度不超过80的字符串。字符串由若干单词和若干空格组成,其中单词是由英文字母(大小写有区分)组成的字符串,单词之间用1个空格分开,输入保证句子末尾没有多余的空格。
输出格式:每个测试用例的输出占一行,输出倒序后的句子。
输入样例:Hello World Here I Come输出样例:
Come I Here World Hello
刚开始想着用结构或者二维数组来解决,但是都觉得太麻烦,最后想到了指针。策略就是用strrchr函数找到最后的空格,然后进行输出,再把空格替换为‘/0’,字符串就把最后面的尾巴给剔除掉了。要注意的点就是第一个单词的处理和输入只有一个单词的情况。代码如下:
#include<stdio.h>#include<string.h>int main(){char s[81]={};gets(s);if(strchr(s,' ')==NULL)//考虑只有一个单词,即字符串中没有空格 {printf("%s\n",s);}else{while(strrchr(s,' ')!=NULL){printf("%s ", strrchr(s,' ')+sizeof(char));// 打印最后一个单词,注意要加 sizeof(char) *strrchr(s,' ')=0;// 最后一个空格改为‘/0’ }printf("%s",s);// 最后一个单词直接打印字符串 }return 0;}
0 0
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文) 1009.说反话
- PAT (Basic Level) Practise (中文)1009说反话 (20)
- PAT (Basic Level) Practise-1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)—— 1009. 说反话
- PAT BASIC LEVEL 1009. 说反话 (20)
- PAT Basic Level 1009. 说反话(20)
- PAT (Basic Level)1009. 说反话
- PAT.Basic Level. T.1009.说反话
- 【C++】浙大PAT (Basic Level)1009. 说反话 (20)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)--1001
- PAT (Basic Level) Practise (中文)
- PAT (Basic Level) Practise (中文)1001
- PAT (Basic Level) Practise (中文)1002
- 《Spring技术内幕》学习笔记6——IoC容器的高级特性
- UICollectionview实现类似UITableview的sectionheader悬浮代码
- Spring基于 Annotation 的简单介绍
- 深入JavaIO和Google GuavaIO文件复制性能小测
- 编译并使用Boost库(Win7+Boost1.53+VS2012)
- PAT (Basic Level) Practise (中文)1009. 说反话 (20)
- 深搜专题初步-1001
- Spring对注解(Annotation)处理源码分析1——扫描和读取Bean定义
- jconsole入门介绍
- VS出现运行对话框程序 pThread->m_pMainWnd->DestroyWindow(); 错误
- Java,php,python,js 定义多行字符串
- Tomcat日志系统详解
- 在这里写文章居然需要审核??
- 关于java程序加图片不能打包成可运行jar的问题