Java program to find frequency of characters in a string

Java program to find frequency of characters in a string


import java.io.*;
class Counting
{
public int countLetters(String nam,char ch)
{ int a=nam.length();int ind=0;
for(int i=0;i<a;i++)
{char c=nam.charAt(i);
if(ch==c)
{ind++;}
}
if(ind==0)
{ind=-1;}
System.out.println(ind);
        return(ind);
    }
    public static void main()throws IOException
    {BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
        System.out.println("Enter a string");
        String n=br.readLine();
        System.out.println("Enter a character ");
       char c=(char)System.in.read();
        Counting obj=new Counting();
        obj.countLetters(n,c);
    }
}

Java program to find frequency of characters in a string
Output;-   Enter a string    I love bluejprog
                 Enter a character  e
                 2


Java program to find frequency of characters in a string
             Java program to find frequency of characters in a string  

No comments:

Powered by Blogger.