博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数...
阅读量:6881 次
发布时间:2019-06-27

本文共 1282 字,大约阅读时间需要 4 分钟。

import java.util.Scanner;/** * @author:(LiberHome) * @date:Created in 2019/3/6 21:04 * @description: * @version:$ *//*已知一个字符串S 以及长度为n的字符数组a,编写一个函数,统计a中每个字符在字符串中的出现次数* 要求函数用s,a,n为参数,返回值为一维整形数组*/public class CountTimes {    public static void main(String[] args) {        System.out.println("请输入一段字符串");        Scanner scanner = new Scanner(System.in);        String s = scanner.nextLine();        char[] a = {'a','b','c','a',};        int n = a.length;        int[] result = new int[n];        result=countTiming(s,a,n);        show(result);    }    private static void show(int[] arrs) {        for (int i = 0; i < arrs.length; i++) {            System.out.print(" "+arrs[i]+"times");        }    }    private static int[] countTiming(String ss, char[] aa, int nn) {        /*计算数组a中每个元素的出现次数*/        int[] answer = new int[nn];        char[] ssArr = ss.toCharArray();        int count=0;        /*将aa数组里面的每一个元素作为对比目标,扫描一遍ssArr数组,将扫描到的次数存放在一个数组中最后返回*/        for (int i = 0; i < aa.length; i++) {            for (int j = 0; j < ssArr.length; j++) {                if (ssArr[j]==aa[i]){                    count++;                }            }            answer[i]=count;            count=0;        }        return answer;    }}

 

转载于:https://www.cnblogs.com/liberhome/p/10486391.html

你可能感兴趣的文章
hdu 1693 : Eat the Trees 【插头dp 入门】
查看>>
nginx安装与fastdfs配置--阿里云
查看>>
wordpress通过代码禁用IE8, IE9,IE10等IE浏览器兼容视图模式(Compatibility View)
查看>>
This application failed to start because it could not find or load the Qt platform plugin "windows"
查看>>
CSS3展现精彩的动画效果 css3的动画属性
查看>>
JSON+JSONP(加量不加价)
查看>>
windows下安装ubuntu,并用win引导ubuntu启动
查看>>
java开发常用工具
查看>>
在VMware Vcenter添加一块网卡后,启动虚机找不到网卡,发现有一个ens38(redhat7.5)...
查看>>
static 关键字和类的加载顺序
查看>>
安卓ListView基础应用
查看>>
【原创】PostgreSQL 快速创建空表TIPS
查看>>
利用PowerBI结合SCOM展示数据报表
查看>>
中学时代的记忆---老师的黑板
查看>>
Horizon View 6-View Connection Server部署⑴
查看>>
iptables 实战演练
查看>>
Python 学习笔记 - 线程(线程锁,信标,事件和条件)
查看>>
RHEL6基础四十一之selinux和iptables基础
查看>>
数据结构之单链表在第i个元素之前插入元素的算法
查看>>
Exchange Server 运维管理02:邮箱数据库存储原理
查看>>