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);
}
/**
* 会将数组里面的单词 倒序排列 例如 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=='\''){ //简单的判断了一下是否是连续的单词
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 数组
* @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--;
}
}
机械节能产品生产企业官网模板...
大气智能家居家具装修装饰类企业通用网站模板...
礼品公司网站模板
宽屏简约大气婚纱摄影影楼模板...
蓝白WAP手机综合医院类整站源码(独立后台)...苏ICP备2024110244号-3 苏公网安备32050702011978号 增值电信业务经营许可证编号:苏B2-20251499 | Copyright 2018 - 2026 源码网商城 (www.yuanmawang.com) 版权所有