/*
Count occurrence of each character of given string
Input:-"Sriman java group"
Output:- S-1, r-2, i-1, m-1, a-3, n-1, J-1, v-1, G-1, o-1, u-1, p-1
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharOccurance
{
public static void main(String[] args)
{
String s = "Sriman Java Group";
String s2=null;
String s3=new String();
Pattern p=null; Matcher m=null;
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
int count=0;
if(!(s3.contains(ch)) && !(ch.equals(" ")))
{
p=Pattern.compile(ch);
m=p.matcher(s);
while(m.find())
{
count++;
s2=m.group();
s3=s3.concat(s2);
}
System.out.println(s2+" - "+count);
}
}
}
}
Count occurrence of each character of given string
Input:-"Sriman java group"
Output:- S-1, r-2, i-1, m-1, a-3, n-1, J-1, v-1, G-1, o-1, u-1, p-1
*/
import java.util.regex.Matcher;
import java.util.regex.Pattern;
public class CharOccurance
{
public static void main(String[] args)
{
String s = "Sriman Java Group";
String s2=null;
String s3=new String();
Pattern p=null; Matcher m=null;
for(int i=0;i<s.length();i++)
{
String ch=String.valueOf(s.charAt(i));
int count=0;
if(!(s3.contains(ch)) && !(ch.equals(" ")))
{
p=Pattern.compile(ch);
m=p.matcher(s);
while(m.find())
{
count++;
s2=m.group();
s3=s3.concat(s2);
}
System.out.println(s2+" - "+count);
}
}
}
}
0 Comment to "WAP in Java to count occurrence of each character of given string."
Post a Comment