寫一個 java 程式,逆轉字串中每個單詞的 tOGGLE?
要執行逆轉並切換對字串中單詞的拆分,請使用 split() 方法對每個單詞進行逆轉,將每個單詞的第一個字母更改為小寫,並將其餘字母更改為大寫。
例子
import java.lang.StringBuffer;
public class ToggleReverse {
public static void main(String args[]){
String sample = "Hello How are you";
String[] words = sample.split(" ");
String result = "";
for(String word:words){
StringBuffer s = new StringBuffer(word);
word = s.reverse().toString();
String firstSub = word.substring(0, 1);
String secondSub = word.substring(1);
result = result+firstSub.toLowerCase()+secondSub.toUpperCase()+" ";
}
System.out.println(result);
}
}輸出
oLLEH wOH eRA uOY
廣告
資料結構
網路技術
RDBMS
作業系統
Java
iOS
HTML
CSS
Android
Python
C 程式設計
C++
C#
MongoDB
MySQL
Javascript
PHP