利用java實(shí)現(xiàn)單詞倒序排列
本文就是會(huì)將數(shù)組里面的單詞進(jìn)行倒序排列 例如 how old are you -> you are old how
示例程序輸出結(jié)果:
the first:
How old are you !? I don't understand
the second:
understand don't I ?! you are old How
示例代碼
public static void main(String[] args) {
char[] chars= new String("How old are you !? I don't understand").toCharArray();
System.out.println("the first:");
System.out.println(chars);
reverseWords(chars); //主要方法
System.out.println("the second:");
System.out.println(chars);
}
/**
* 會(huì)將數(shù)組里面的單詞 倒序排列 例如 how old are you -> you are old how
* @param chars
*/
public static void reverseWords(char[] chars) {
reverseChars(chars,0,chars.length-1);
int begin = -1;
int end = 0;
for(int i=0;i<chars.length;i++){
char c = chars[i];
if((c>='a'&&c<='z')||(c>='A'&&c<='Z')||c=='\''){ //簡(jiǎn)單的判斷了一下是否是連續(xù)的單詞
if(begin==-1){
begin = i;
end=i;
}else{
end=i;
if(i==chars.length-1){
reverseChars(chars,begin,end);
}
}
}else{
if(begin!=-1){
reverseChars(chars,begin,end);
begin=-1;
end=0;
}
}
}
}
/**
* 將char 一定范圍內(nèi)的 字符 倒序排列 例如 hello -> olleh
* @param chars 數(shù)組
* @param begin 開始位置
* @param end 結(jié)束位置
*/
public static void reverseChars(char[] chars, int begin, int end) {
while(end>begin){
char c = chars[begin];
chars[begin] = chars[end];
chars[end] = c;
begin++;
end--;
}
}
以上就是利用java實(shí)現(xiàn)單詞倒序排列,希望對(duì)大家能夠理解,對(duì)大家有所幫助
相關(guān)文章
SpringCloud實(shí)現(xiàn)Eureka服務(wù)注冊(cè)與發(fā)現(xiàn)
這篇文章主要介紹了SpringCloud如何實(shí)現(xiàn)Eureka服務(wù)注冊(cè)與發(fā)現(xiàn),幫助大家更好的理解和學(xué)習(xí)使用SpringCloud,感興趣的朋友可以了解下2021-05-05
在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解
這篇文章主要介紹了在IntelliJ IDEA 搭建springmvc項(xiàng)目配置debug的教程詳解,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-09-09
java實(shí)現(xiàn)十六進(jìn)制字符unicode與中英文轉(zhuǎn)換示例
當(dāng)需要對(duì)一個(gè)unicode十六進(jìn)制字符串進(jìn)行編碼時(shí),首先做的應(yīng)該是確認(rèn)字符集編碼格式,在無法快速獲知的情況下,通過一下的str4all方法可以達(dá)到這一目的2014-02-02
深入了解Java中循環(huán)結(jié)構(gòu)的使用
Java中有三種主要的循環(huán)結(jié)構(gòu):while 循環(huán)、do…while 循環(huán)和for 循環(huán)。本文將來和大家一起講講Java中這三個(gè)循環(huán)的使用,需要的可以參考一下2022-08-08
springboot調(diào)用python腳本的實(shí)現(xiàn)示例
本文介紹了在SpringBoot應(yīng)用中調(diào)用Python腳本,包括ProcessBuilder類和ApacheCommonsExec庫兩種方法,具有一定的參考價(jià)值,感興趣的可以了解一下2024-12-12
Java實(shí)現(xiàn)異步延遲隊(duì)列的方法詳解
目前系統(tǒng)中有很多需要用到延時(shí)處理的功能,本文就為大家介紹了Java實(shí)現(xiàn)異步延遲隊(duì)列的方法,文中的示例代碼講解詳細(xì),需要的可以參考一下2023-03-03
基于Java在netty中實(shí)現(xiàn)線程和CPU綁定
這篇文章主要介紹了基于Java在netty中實(shí)現(xiàn)線程和CPU綁定,文章圍繞主題的相關(guān)內(nèi)容展開詳細(xì)介紹,具有一定的參考價(jià)值,需要的小伙伴可以參考一下2022-05-05
Kotlin語言編程Regex正則表達(dá)式實(shí)例詳解
這篇文章主要為大家介紹了Kotlin語言編程Regex正則表達(dá)式實(shí)例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-08-08

