program to count frequency of a given Letter in a String

Program:
import java.util.*;
import java.lang.*;
class count
{
public static void main(String[] args)
{
int n,k=0,i;
String str = "";
Scanner sc = new Scanner(System.in);
System.out.println("Enter String: ");
str = sc.nextLine();
n = str.length();
System.out.println("Enter character whose occurences to count: ");
char ch=sc.next().charAt(0);
char c[] = new char[n];
c = str.toCharArray();
for(i=0;i<n;i++)
{
if(c[i]==ch)
k++;
}
System.out.println("Total occurences = "+k+" times.");
}
Output:


Comments

Popular posts from this blog

Sequential File Allocation

Indexed File Allocation method

Linked File Allocation