Tuesday, December 28, 2021

Print Number of occurrences of each character from a string.

 Print the Number of occurrences of character form a string, we have many solution to solve this.

Here I posted a simple way I used to print number of occurrences of each character. 


private static void occurrenceOfChar(String input) {
int length = input.length();
char[] inputArray = input.toCharArray();
while(input.length() > 0){
input = input.replace(inputArray[0]+"", "");
System.out.println(inputArray[0] +" Occurrences is " + (length - input.length()));
inputArray = input.toCharArray();
length = input.length();
}
}
Input : Helloworld
Output : 
H Occurrences is 1
e Occurrences is 1
l Occurrences is 3
o Occurrences is 2
W Occurrences is 1
r Occurrences is 1
d Occurrences is 1


No comments:

Post a Comment