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;
}
}