![](/img/trans.png)
[英]Count the occurrence of each character in a string and put it in an array
[英]I want to find Java program to count the occurrence of each character in a string without using Hashmap or array concept
package com.stringprogramdemo;
public class OccuranceOfCharacterIngeneral {
public static void main(String args[]) {
String str="Reshma Raghunath Bangar";
int count=0;
for(int i=0;i<str.length();i++) {
for(int j=i;j<str.length();j++) {
if(str.charAt(i)==str.charAt(j)) {
count++;
}else {
}
//System.out.println("Total number of count of occurance of "+str.charAt(i)+" is "+count);
}
System.out.println("Total number of count of occurance of "+str.charAt(i)+" is "+count);
}
}
}
Output:
Total number of count of occurance of R is 2
Total number of count of occurance of e is 3
Total number of count of occurance of s is 4
Total number of count of occurance of h is 7
Total number of count of occurance of m is 8
Total number of count of occurance of a is 13
Total number of count of occurance of is 15
Total number of count of occurance of R is 16
Total number of count of occurance of a is 20
Total number of count of occurance of g is 22
Total number of count of occurance of h is 24
Total number of count of occurance of u is 25
Total number of count of occurance of n is 27
Total number of count of occurance of a is 30
Total number of count of occurance of t is 31
Total number of count of occurance of h is 32
Total number of count of occurance of is 33
Total number of count of occurance of B is 34
Total number of count of occurance of a is 36
Total number of count of occurance of n is 37
Total number of count of occurance of g is 38
Total number of count of occurance of a is 39
Total number of count of occurance of r is 40
public static void main(String args[]) {
String str="Reshma Raghunath Bangar";
for(int i=0;i<str.length();i++) {
int count=0; // move count to here so it counts from zero every iteration of outer loop
for(int j=i;j<str.length();j++) {
if(str.charAt(i)==str.charAt(j)) {
count++;
}
}
System.out.println("Total number of count of occurance of "+str.charAt(i)+" is "+count);
}
公共 class 應用程序{
public static void main(String args[]) {
String str=" Reshma Raghunath Bangar";
String completed = "";
for(int i=0;i<str.length();i++) {
int count=0;
for(int j=i; j<str.length();j++) {
if(str.charAt(i)==str.charAt(j)) {
count++;
}
}
String s = String.valueOf(str.charAt(i));
if(!completed.contains(s)){
completed=completed+str.charAt(i);
System.out.println("Total number of count of occurance of "+str.charAt(i)+" is "+count);
}
}
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.