- How to print duplicate characters from the string with its count.?
public class Demo1 {
public static void main(String[] args) {
String s="sateesh";
int count=0,len=0;
while(len!=1)
{
char name[]=s.toCharArray();
len=name.length;
count=0;
for(int j=0;j<len;j++)
{
if((name[0]==name[j])&&((name[0]>=65&&name[0]<=91)|| (name[0]>=97&&name[0]<=123)))
count++;
}
if(count!=0)
System.out.println(name[0]+" "+count+" Times");
s=s.replace(""+name[0],"");
}
}
}
Output:
Char: S, Count: 2 times.
Char: a, Count: 1 times.
Char: t, Count: 1 times.
Char: e, Count: 2 times.
Char: h, Count: 1 times.
more programs visit http://sateeshm.com/