"Geeksforgeeks"
。G:2 e:4 k:2 s:2 f:1 o:1 r:1
public static void main(String[] args)
{
String str = "Geeksforgeeks"; ``
char[] ch = new char[str.length()];
int k=0;
int[] f = new int[str.length()];
for(int i = 0;i<str.length();i++){
int add= 0;
ch[k] = str.charAt(i);
int flag =-1;
for(int p = 0;p<k;p++)
{
if(ch[p].equalsIgnoreCase(str.charAt(i)))
{
flag++;
break;
}
}
if(flag == -1)
{
for(int j=i;j<str.length();j++)
{
if(ch[k].equalsIgnoreCase(str.charAt(j)))
{
add++;
}
}
f[k] = add;
k++;
}
}
for(int i = 0;i<k;i++)
{
System.out.println(ch[i]+ " "+f[i]);
}
}
答案 0 :(得分:0)
char是原始类型,不能在其上使用equalsIgnoreCase。
要解决此问题,您可以将其解析为字符串:
String.valueOf(ch[p]).equalsIgnoreCase(str.charAt(i));
答案 1 :(得分:0)
您可以像这样编写您的课程:
public class TestClass {
static final int MAX_CHAR = 256;
static void getOccuringChar(String str)
{
// 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)]);
}
}
}
和主要方法中:
public static void main(String[] args){
String str = "geeksforgeeks";
TestClass.getOccuringChar(str);
}
结果将是这样的:
Number of Occurrence of g is:2
Number of Occurrence of e is:4
Number of Occurrence of k is:2
Number of Occurrence of s is:2
Number of Occurrence of f is:1
Number of Occurrence of o is:1
Number of Occurrence of r is:1