最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

利用java實現(xiàn)單詞倒序排列

 更新時間:2015年07月30日 16:15:25   作者:Ds_Diamond  
這篇文章就是利用java實現(xiàn)單詞倒序排列,感覺像是在變魔術,感興趣的小伙伴來見證一下

本文就是會將數(shù)組里面的單詞進行倒序排列 例如 how old are you -> you are old how

示例程序輸出結果:

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);
  }
 
   
  /**
   * 會將數(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=='\''){ //簡單的判斷了一下是否是連續(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 一定范圍內的 字符 倒序排列 例如   hello -> olleh
   * @param chars 數(shù)組
   * @param begin 開始位置
   * @param end  結束位置
   */
  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實現(xiàn)單詞倒序排列,希望對大家能夠理解,對大家有所幫助

相關文章

最新評論

翼城县| 上饶县| 攀枝花市| 山西省| 巢湖市| 阿坝县| 民乐县| 都昌县| 商丘市| 威海市| 昌图县| 邵阳县| 宣汉县| 南溪县| 黄大仙区| 安远县| 东丽区| 余姚市| 安义县| 蒲江县| 平乡县| 福贡县| 峨眉山市| 威海市| 固阳县| 古蔺县| 灵石县| 乐业县| 东乌珠穆沁旗| 沈阳市| 聂荣县| 太谷县| 河南省| 礼泉县| 深水埗区| 玉溪市| 体育| 安远县| 丽江市| 山阳县| 绥化市|