Frequency Of A Letter In A String

Frequency Of A Letter In A String


Write a program to print the no of times letter 'K' appears in a string.

/* WAP to print the no of times letter 'K' appears in a string.  */
import java.io.*;
class StringQ05
{
public static void main(String arg[])throws IOException
{
 int i, l, ctr;
 String str;
 char ch;

 BufferedReader br=new BufferedReader(new InputStreamReader(System.in));
    System.out.println("Enter a string : ");
    str=br.readLine();
    l = str.length();

    // To count the letter 'K'
    ctr = 0; 
    for(i=0;i < l;i++){
        ch = str.charAt(i);

        //Count the uppercase letter 'K'
        if(ch == 'K')
            ctr++;
    }
    System.out.println("No of times letter 'K' appears in the string: "+ctr);

}//end of main
}//end of class

Have something to say? Log in to comment on this post.

0 comments