2 求字符串分割

题目:


给定一个非空字符串S,其被N个-'分隔成N+1的子串,给定正整数K,要求除第一个子串外,其余的子串每K个字符组成新的子串,并用-'分隔。
对于新组成的每一个子串,如果它含有的小写字母比大写字母多,则将这个子串的所有大写字母转换为小写字母;
反之,如果它含有的大写字母比小写字母多,则将这个子串的所有小写字母转换为大写字母;大小写字母的数量相等时,不做转换。

输入描述
输入为两行,第一行为参数K,第二行为字符串S。

输出描述
输出转换后的字符串。

package com.company;

import java.util.Scanner;

public class Main {


    public static void main(String[] args) {
        //test one


        Scanner sc = new Scanner(System.in);
        String str = sc.nextLine();
        int k = sc.nextInt();

        String[] tempStr = str.split("-");
        StringBuilder stringBuilder = new StringBuilder();

        stringBuilder.append(tempStr[0]);

        StringBuilder sj = new StringBuilder();
        for (int i = 1; i < tempStr.length; i++) {
            sj.append(tempStr[i]);
        }

        String newStrs = sj.toString();

        for (int i = 0; i < newStrs.length(); i += k) {

            String tempStrs = newStrs.substring(i, Math.min(i + k, newStrs.length()));
            sj.append(Convert(tempStrs));
        }

       //System.out.println(sj.toString());
        for (int i=0;i<sj.length();i++){
            System.out.println(sj);
        }
    }

    public static String Convert(String ss) {

        int m = 0;
        int n = 0;
        for (int i = 0; i < ss.length(); i++) {
            char s = ss.charAt(i);
            if (s > 'a' && s < 'z') {
                m++;
            }
            if (s > 'A' && s < 'Z') {
                n++;
            }
        }
        if (m == n) return ss;
        else if (m > n) {
            return ss.toLowerCase();
        } else {
            return ss.toUpperCase();
        }
    }
}

  • 13
    点赞
  • 9
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值