你们可能会嘲笑我实现这个简单任务的方式。我知道有一种更简单的方法可以做到这一点,但我现在真的无法想到它。你能帮帮我吗?
String sample = "hello world";
char arraysample[] = sample.toCharArray();
int length = sample.length();
//count the number of each letter in the string
int acount = 0;
int bcount = 0;
int ccount = 0;
int dcount = 0;
int ecount = 0;
int fcount = 0;
int gcount = 0;
int hcount = 0;
int icount = 0;
int jcount = 0;
int kcount = 0;
int lcount = 0;
int mcount = 0;
int ncount = 0;
int ocount = 0;
int pcount = 0;
int qcount = 0;
int rcount = 0;
int scount = 0;
int tcount = 0;
int ucount = 0;
int vcount = 0;
int wcount = 0;
int xcount = 0;
int ycount = 0;
int zcount = 0;
for (int i = 0; i < length; i++) {
char c = arraysample[i];
switch (c) {
case 'a':
acount++;
break;
case 'b':
bcount++;
break;
case 'c':
ccount++;
break;
case 'd':
dcount++;
break;
case 'e':
ecount++;
break;
case 'f':
fcount++;
break;
case 'g':
gcount++;
break;
case 'h':
hcount++;
break;
case 'i':
icount++;
break;
case 'j':
jcount++;
break;
case 'k':
kcount++;
break;
case 'l':
lcount++;
break;
case 'm':
mcount++;
break;
case 'n':
ncount++;
break;
case 'o':
ocount++;
break;
case 'p':
pcount++;
break;
case 'q':
qcount++;
break;
case 'r':
rcount++;
break;
case 's':
scount++;
break;
case 't':
tcount++;
break;
case 'u':
ucount++;
break;
case 'v':
vcount++;
break;
case 'w':
wcount++;
break;
case 'x':
xcount++;
break;
case 'y':
ycount++;
break;
case 'z':
zcount++;
break;
}
}
System.out.println("There are " + hcount + " h's in here ");
System.out.println("There are " + ocount + " o's in here ");
答案 0 :(得分:3)
尝试使用使用ASCII代码的数组:
int[] counters = new int[0x100];
for(int i = 0; i < string.length(); i++) {
char c = string.charAt(i);
counters[(int) c]++;
}
for(char c = 'a'; c <= 'z'; c++) {
System.out.println("There are "+counters[(int) c]+" "+c+"'s in the String.");
}
你所做的就是获取正确的角色(就像你已经做过的那样)。现在每个角色都有一个数字。你可以在这里查看它们:https://en.wikipedia.org/wiki/ASCII。然后你增加相关的计数器。
一些注意事项(作为对Chuck Lowery评论的回应)
0x100
是十六进制数字,等于256
可能的字符数。请参阅jdoodle。
答案 1 :(得分:0)
你可以创建一个函数,使用正则表达式来提取包含作为参数给定的给定字母的所有出现的串联的字符串,并返回该字符串的长度。
编辑:以下页面也可能有用:Java: How do I count the number of occurrences of a char in a String?
编辑#2:当然,你的方法的好处是你只需要遍历字符串一次。
答案 2 :(得分:0)
public void countChars(String str) {
int numOfChars = 'z' - 'a' + 1;
int[] arr = new int[numOfChars];
for (int i = 0; i < str.length(); i++) {
arr[str.charAt(i) - 'a']++;
}
System.out.println("There are " + arr['z' - 'a'] + " z's in here ");
}
答案 3 :(得分:0)
使用索引对应于每个字符的数组
String str = "Hello World";
String strLower = str.toLowerCase();
int[] charCounts = new int[26];
for (int i = 0; i < strLower.length(); i++) {
char c = strLower.charAt(i);
int charIndex = (int)c - 97
charCounts[charIndex]++;
}
答案 4 :(得分:0)
您应该将'count'存储在一个容器中。在这种情况下,我认为Map
最合适。使用地图可以根据任何对象的Key:Value对存储一条信息。在这种情况下,您的密钥可以是字符(char
类型),例如'a'
或'z'
,您的值将是该字符(int
类型)的标记。您可以在Java API here中阅读有关Maps的更多信息。
示例实现是
Map m = new Map();
m.put('c',11); //this puts the Value eleven into the map for Key 'c'
m.get('c'); //this would return the value stored for char 'c', in this case 11
如果您更习惯使用Arrays,则可以存储每个char值,以使'a'计数的索引为[0],'b'计数的索引为[1]。或者你可以用每个字符的ASCII值索引数组,尝试类似char ch = (int)'a';
的东西,它应该返回97.