标签:pac 实现 length reverse ringbuf 转换 get size string
1、编程实现将字符串先进行大小写互转,再进行字符串反转。
输入:AuJKtHyKy
输出:YkYhTkjUa
1 package com.fhcq.reverseAndChange; 2 3 /* 4 * 编程实现将字符串先进行大小写互转,再进行字符串反转。 5 * 输入:AuJKtHyKy 6 * 输出:YkYhTkjUa 7 */ 8 public class ReverseAndChange { 9 10 public static void main(String[] args) { 11 String s = "AuJKtHyKy"; 12 reverseAndChange(s); 13 } 14 15 public static void reverseAndChange(String str) { 16 17 char[] ch = str.toCharArray();// 字符串转换为字符数组 18 StringBuffer sb = new StringBuffer(); 19 for (int i = 0; i < ch.length; i++) { 20 if (ch[i] >= ‘a‘ && ch[i] <= ‘z‘) { 21 sb.append(String.valueOf(ch[i]).toUpperCase()); 22 } else if (ch[i] >= ‘A‘ && ch[i] <= ‘Z‘) { 23 sb.append(String.valueOf(ch[i]).toLowerCase()); 24 } 25 } 26 String result = sb.reverse().toString();// 将sb逆序,并转换为字符串形式 27 System.out.println(result); 28 } 29 30 } 31
2、请编写一段代码,将下面的句子的语序颠倒:
“Long long ago , there lived a king”。
(提示:即把上述句子变成“king a lived there , ago long Long”)。
1 package com.fhcq.sentenceReverse; 2 3 /** 4 * 请编写一段代码,将下面的句子的语序颠倒: 5 * “Long long ago , there lived a king”。 6 * (提示:即把上述句子变成“king a lived there , ago long Long”)。 7 */ 8 import java.util.ArrayList; 9 import java.util.List; 10 11 public class SentenceReverse { 12 public static void main(String[] args) { 13 14 String str = "Long long ago , there lived a king"; 15 16 List<String> list = new ArrayList<String>(); 17 18 StringBuffer buffer = new StringBuffer(str).reverse(); 19 String[] strings = buffer.toString().split(" "); 20 21 for (String string : strings) { 22 StringBuilder builder = new StringBuilder(string).reverse(); 23 list.add(builder.toString()); 24 } 25 26 for (int i = 0; i < list.size(); i++) { 27 System.out.print(list.get(i) + " "); 28 } 29 } 30 } 31
标签:pac 实现 length reverse ringbuf 转换 get size string
原文地址:http://www.cnblogs.com/justlove/p/7808240.html