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:
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
Post a Comment