【发布时间】:2018-11-05 18:12:57
【问题描述】:
这是参考代码:
// Create an array of size 256 i.e. ASCII_SIZE
int count[] = new int[MAX_CHAR];
int len = str.length();
// Initialize count array index
for (int i = 0; i < len; i++)
count[str.charAt(i)]++;
// Create an array of given String size
char ch[] = new char[str.length()];
for (int i = 0; i < len; i++) {
ch[i] = str.charAt(i);
int find = 0;
for (int j = 0; j <= i; j++) {
// If any matches found
if (str.charAt(i) == ch[j])
find++;
}
if (find == 1)
System.out.println("Number of Occurrence of " +
str.charAt(i) + " is:" + count[str.charAt(i)]);
}
输出应该类似于:
“x”出现的次数是:“它出现的次数”
如果该字母以前出现过,则只显示该出现一次。
我使用 2 个 for 循环得到了逻辑,虽然我的老师说它可以只使用 1 个 for 循环来执行这个应用程序。
我遇到的问题是:
只有当角色彼此相邻时,我才能找到是否已经找到角色。
如果没有另一个 for 循环,您如何检查是否找到了所有先前的字符?
【问题讨论】:
-
您是否找到特定字符或该字符串中所有字符的出现?
-
查找字符串中的所有字符
-
检查我的答案