码迷,mamicode.com
首页 > 其他好文 > 详细

【字符串】实现字符串的翻转

时间:2018-07-08 21:13:50      阅读:164      评论:0      收藏:0      [点我收藏+]

标签:style   color   字符串翻转   span   append   col   ati   字符   sys   

 1 import java.util.Scanner;
 2 
 3 /**
 4  * 功能:字符串翻转,例如how are you 翻转为you are how
 5  */
 6 public class Main5 {
 7 
 8     public String swap(String string) {
 9 
10         if (string == null) {
11             return null;
12         }
13 
14         String[] strings = string.split(" ");
15 
16         String temp;
17         for (int i = 0; i < strings.length / 2; i++) {
18             temp = strings[i];
19             strings[i] = strings[strings.length - 1 - i];
20             strings[strings.length - 1 - i] = temp;
21         }
22 
23         StringBuilder stringBuilder = new StringBuilder();
24         for (int i = 0; i < strings.length; i++) {
25             if (i != 0) {
26                 stringBuilder.append(" ");
27             }
28             stringBuilder.append(strings[i]);
29         }
30 
31         return stringBuilder.toString();
32     }
33 
34     public static void main(String[] args) {
35 
36         Scanner scanner = new Scanner(System.in);
37         Main5 main5 = new Main5();
38 
39         while (scanner.hasNextLine()) {
40             String string = scanner.nextLine();
41             System.out.println(main5.swap(string));
42         }
43     }
44 }

 

【字符串】实现字符串的翻转

标签:style   color   字符串翻转   span   append   col   ati   字符   sys   

原文地址:https://www.cnblogs.com/jiangyi-uestc/p/9281168.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!