栏目分类:
子分类:
返回
名师互学网用户登录
快速导航关闭
当前搜索
当前分类
子分类
实用工具
热门搜索
名师互学网 > IT > 软件开发 > 后端开发 > Java

利用java实现单词倒序排列

Java 更新时间: 发布时间: IT归档 最新发布 模块sitemap 名妆网 法律咨询 聚返吧 英语巴士网 伯小乐 网商动力

利用java实现单词倒序排列

本文就是会将数组里面的单词进行倒序排列 例如 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);
  }
 
   
  
  public static void reverseWords(char[] chars) {
    reverseChars(chars,0,chars.length-1);
    int begin = -1;
    int end = 0;
    for(int i=0;i='a'&&c<='z')||(c>='A'&&c<='Z')||c=='''){ //简单的判断了一下是否是连续的单词
 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;
 }
      }
    }
  }
 
  
  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实现单词倒序排列,希望对大家能够理解,对大家有所帮助

转载请注明:文章转载自 www.mshxw.com
本文地址:https://www.mshxw.com/it/151342.html
我们一直用心在做
关于我们 文章归档 网站地图 联系我们

版权所有 (c)2021-2022 MSHXW.COM

ICP备案号:晋ICP备2021003244-6号