Thursday, August 23, 2018

How to count occurrences of each character of a string without using Regular Expression in java?

package com.altafjava.coos.test;

public class CountOccurrences2 {
public static void main(String[] args) {
String s="annotation";
System.out.println("Original String = "+s);

while(s.length()!=0){
int count=0;
for(int i=0;i<s.length();i++){
if(s.charAt(0)==s.charAt(i))
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 without using Regular Expression in java?"

Post a Comment