8.22 字符串统计 toCharArray

统计字符串中"n","o"出现的次数

class StringUtil {
    // 返回第一个内容为字母n的个数,第二个内容为字母o的个数
    public static int[] count(String str){
        int countData[] = new int[2];
        char[] data = str.toCharArray();//将字符串变成字符数组,其中空格也占位

        for(int x = 0;x < data.length;x++){
            //System.out.print(data[x]);//want you to know one thing
            if(data[x] == 'n' || data[x] == 'N'){
                countData[0]++;
            }

            if(data[x] == 'o' || data[x] == 'O'){
                countData[1]++;
            }
        }
        return countData;
    }
}

public class HelloWorld {
	public static void main(String args[]) {
        String str = "want you to know one thing";
        int result[] = StringUtil.count(str);
        System.out.println("字母n的个数:" + result[0]);
        System.out.println("字母o的个数:" + result[1]);
	}
}
posted @   盘思动  阅读(13)  评论(0编辑  收藏  举报
相关博文:
阅读排行:
· 无需6万激活码!GitHub神秘组织3小时极速复刻Manus,手把手教你使用OpenManus搭建本
· C#/.NET/.NET Core优秀项目和框架2025年2月简报
· Manus爆火,是硬核还是营销?
· 终于写完轮子一部分:tcp代理 了,记录一下
· 【杭电多校比赛记录】2025“钉耙编程”中国大学生算法设计春季联赛(1)
历史上的今天:
2022-06-08 tp3.2 企业转账到零钱
点击右上角即可分享
微信分享提示