Thursday, August 23, 2018

How to count occurrences of each character of a string in java?

package com.altafjava.coocis.test;

import java.util.regex.Matcher;
import java.util.regex.Pattern;

public class CountCountOccurrences {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);
while(s.length()!=0){
int count=0;
Pattern p=Pattern.compile(s.charAt(0)+"");
Matcher matcher=p.matcher(s);
while(matcher.find()){
count++;
}
System.out.println(s.charAt(0)+" = "+count);
s=s.replaceAll(s.charAt(0)+"","");
}
}
}




How to count occurrences of each character in a string in java?



Share this

0 Comment to "How to count occurrences of each character of a string in java?"

Post a Comment