按指定格式拼接字符串,统计输入字符串中各种字符个数

按指定格式拼接字符串

*给定数组{1,2,3},输出[word1#word2#word3]*/
public class demoStringPractice {
    public static void main(String[] args) {
        System.out.println(getString(new int[] {1,2,3}));
    }
    public static String getString(int[] array) {
        String result = "[";
        for (int i = 0; i <array.length ; i++) {
            if (i==array.length-1){
                result += "word"+array[i]+"]";
            }
            else {
                result += "word"+array[i]+"#";
            }
        }
        return result;
    }
}

统计输入字符串中各种字符个数

import java.util.Scanner;
/*键盘输入一串字符,统计各种类型字符出现次数,类型有大写字母、小写字母、数字、其他*/
public class inputCount {
    public static void main(String[] args) {
        System.out.println("请输入字符串:");
        String str = new Scanner(System.in).next();//得到输入字符串
        int[] result = CountType(str);
        System.out.println("小写字母:"+result[0]+"\n"+
                "大写字母:"+result[1]+"\n"+
                "数字:"+result[2]+"\n"+
                "其它:"+result[3]+"\n");
    }
    public static int[] CountType(String str){
        int[] result = {0,0,0,0};
        for (int i = 0; i < str.length() ; i++) {
            if (97<=str.charAt(i) && str.charAt(i)<=122){
                result[0] += 1;
            }
            else if (65<=str.charAt(i) && str.charAt(i)<=90){
                result[1] += 1;
            }
            else if (48<=str.charAt(i) && str.charAt(i)<=57){
                result[2] += 1;
            }
            else {
                result[3] += 1;
            }
        }
        return result;
    }
}


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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值