Java程序计算给定句子中每个单词的字符出现次数
要计算给定句子中每个单词的字符出现次数,Java代码如下-
示例
import java.util.*;
public class Demo{
static final int max_chars = 256;
static void char_occurence(String my_str){
int count[] = new int[max_chars];
int str_len = my_str.length();
for (int i = 0; i < str_len; i++)
count[my_str.charAt(i)]++;
char ch[] = new char[my_str.length()];
for (int i = 0; i < str_len; i++){
ch[i] = my_str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++){
if (my_str.charAt(i) == ch[j])
find++;
}
if (find == 1)
System.out.println("" + my_str.charAt(i) + " 出现的次数是 :" +
count[my_str.charAt(i)]);
}
}
public static void main(String[] args){
Scanner my_scan = new Scanner(System.in);
String my_str = "This is a sample";
char_occurence(my_str);
}
}输出结果
T 出现的次数是:1 h 出现的次数是:1 i 出现的次数是:2 s 出现的次数是:3 出现的次数是:3 a 出现的次数是:2 m 出现的次数是:1 p 出现的次数是:1 l 出现的次数是:1 e 出现的次数是:1
热门推荐
1 训练狗狗憋尿
2 狗狗追踪训练
3 狗狗训练要多少钱
5 深圳狗狗训练
6 训练狗狗唤回
7 狗狗叫怎么训练
8 狗狗训练班
10 狗狗社会化训练
11 狗狗叫回训练
12 广州狗狗训练学校
13 狗狗训练躺下
14 狗狗随行训练
15 训练狗狗巡回
16 狗狗要怎么训练
17 狗狗喂食训练
18 训练狗狗躺下