Count Occurrences of Characters in String - JAVA
Make a Simple program which count the occurrences of Each Character in String . It will take one STRING as an INPUT.
Example:
INPUT : kapilshukla
OUTPUT :
k – 2
a – 2
p – 1 ….
class prog2
{
public static void main(String args[])
{
char c[]=new char[10];
for(int i=0;i<args[0].length();i++)
{
//This code will work if character is not @
if(args[0].charAt(i)!='@')
{
//Count variable will count occurrences of Character
int count=1;
for(int j=i+1;j<args[0].length();j++)
{
//This code will compare two character
if(args[0].charAt(i)==args[0].charAt(j))
{
count++;
}
}
System.out.println(args[0].charAt(i)+ " - " + count);
}
//This Code will replace all counted character with @
args[0] = args[0].replace(args[0].charAt(i), '@');
}
}
}
No comments:
Post a Comment