当前位置:网站首页>倒排单词
倒排单词
2022-08-02 03:33:00 【小艾菜菜菜】
题目描述:
读入一行英文(只包含字母和空格,单词间以单个空格隔开),将所有的单词的顺序倒排输出,依然以单个空格隔开。
输入格式:
输入为一个字符串(字符串的长度为100).
输出格式:
输出为按要求排序后的字符串。
输入样例:
I am a student
输出样例:
student a am I
解题思路:
就是根据所给的样例进行找规律:我们每次可以寻找空格,找到空格就意味着对应的单词也就找到了,然后颠倒位置输出来就可以了。
代码实现:
package com.aiai.string;
import java.util.Scanner;
public class BackString {
public static void main(String[] args) {
Scanner sc = new Scanner(System.in);
String s = sc.nextLine();
for (int i = s.length()-1 ; i >= 0; i--){
int j = i-1;
while (s.charAt(j) != ' ' && j >= 0) {
j--;
if (j == -1){
//防止字符串溢出
break;
}
}
j ++;
for (int h = j ; h <= i; h++){
System.out.print(s.charAt(h));
}
System.out.print(" ");
i = j -1;
}
}
}
边栏推荐
- 振芯科技GM8285C:功能TTL转LVDS芯片简介
- Process (present) : custom shell command line interpreter
- 2020 - AAAI - Image Inpainting论文导读《Learning to Incorporate Structure Knowledge for Image Inpainting》
- proteus数字电路仿真——入门实例
- 【数据库】事务的四大特性<详解>
- Personal image bed construction based on Alibaba Cloud OSS+PicGo
- 进程(下):进程控制、终止、等待、替换
- 为什么D类音频功放可以免输出滤波器
- 联阳IT66121FN提供SDI转HDMI方案分享
- 联阳IT6561|IT6561FN方案电路|替代IT6561方案设计DP转HDMI音视频转换器资料
猜你喜欢
The use and simulation of vector implementation:
改变文件的扩展名
振芯科技GM8285C:功能TTL转LVDS芯片简介
Introduction and mock implementation of list:list
HDMI转MIPI CSI东芝转换芯片-TC358743XBG/TC358749XBG
Process (below): process control, termination, waiting, replacement
【多线程】线程安全保护机制
MPU6050 accelerometer and gyroscope sensor is connected with the Arduino
【LeetCode】链表相加 进位
Website development plan research
随机推荐
字符串匹配(蛮力法+KMP)
Lightly:新一代的C语言IDE
USB HUB USB集线器电路设计
VCA821可变增益放大器
Comparative analysis of OneNET Studio and IoT Studio
基础IO(下):软硬链接和动静态库
进程(番外):自定义shell命令行解释器
【LeetCode】链表相加 进位
bluez5.50蓝牙文件传输
调试九法准则
【TCS3200 color sensor and Arduino realize color recognition】
【数据库】事务的四大特性<详解>
全加器高进位和低进位的理解
【LeetCode】Merge
bluez5.50+pulseaudio实现蓝牙音响音频播放
开源日志库 [log4c] 使用
Beckhoff ET2000 listener use
Introduction and mock implementation of list:list
Host your own website with Vercel
USB_ID介绍