字符串排序

1 篇文章 0 订阅

 

题目描述

编写一个程序,将输入字符串中的字符按如下规则排序。

规则 1 :英文字母从 A 到 Z 排列,不区分大小写。

如,输入: Type 输出: epTy

规则 2 :同一个英文字母的大小写同时存在时,按照输入顺序排列。

如,输入: BabA 输出: aABb

规则 3 :非英文字母的其它字符保持原来的位置。
 

如,输入: By?e 输出: Be?y

 

注意有多组测试数据,即输入有多行,每一行单独处理(换行符隔开的表示不同行)

import java.util.*;
public class Main{
    public static void main(String[] args){
        Scanner sc = new Scanner(System.in);
        char temp;
        int right;
        while(sc.hasNext()){
            String s = sc.nextLine();
            char[] chars = s.toCharArray();
            for(int i=0;i<chars.length-1;i++){
                for (int j=0;j<chars.length-1-i;j++){
                    if(!isAa(chars[j]))
                        continue;
                    right = j+1;
                    while (right < chars.length && !isAa(chars[right])){
                        right++;
                    }
                    if(right < chars.length && String.valueOf(chars[j]).compareToIgnoreCase(String.valueOf(chars[right]))>0){
                        temp = chars[j];
                        chars[j] = chars[right];
                        chars[right] = temp;
                    }
                }
            }
            System.out.println(String.valueOf(chars));
        }
    }
    public static boolean isAa(char ch){
        if (ch>='a' && ch <='z')
            return true;
        if (ch>='A' && ch <='Z')
            return true;
        return false;
    }
}

 

 

  • 3
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值