Java program: find the frequency of the given character in a string
Java program: find the frequency of the given character in a string
In this article, we will discuss the concept of theJava program: find the frequency of the given character in a string
In this post, we are going to learn how to find the frequency of the given character in the string in Java programming language

Code to find the frequency of the given character in the string
find the frequency of the given character in the string using for loop
The program allows the user to enter a String and a character and then it finds the frequency of the given character in the string using for loop in Java programing language
program 1
//program to count vowel,consonant,digit,special character and space
import java.util.Scanner;
public class FindFrequencyOfChar{
public static void main(String args[]){
//variable declaration
String str;
char ch;
int frequency=0;
//vriable declaration and initialization
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.println("Enter the String ");
str=scan.nextLine();
System.out.println("Enter the Character ");
ch=scan.nextLine().charAt(0);
for(int i=0; iimport java.util.Scanner;
public class FindFrequencyOfChar1{
public static void main(String args[]){
//variable declaration
String str;
char ch;
int frequency=0;
//vriable declaration and initialization
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.println("Enter the String ");
str=scan.nextLine();
System.out.println("Enter the Character ");
ch=scan.nextLine().charAt(0);
int i=0;
while(ido-while loop in Java programing language
program 3
import java.util.Scanner;
public class FindFrequencyOfChar2{
public static void main(String args[]){
//variable declaration
String str;
char ch;
int frequency=0;
//vriable declaration and initialization
Scanner scan=new Scanner(System.in);
//create a scanner object for input
System.out.println("Enter the String ");
str=scan.nextLine();
System.out.println("Enter the Character ");
ch=scan.nextLine().charAt(0);
int i=0;
do{
if(ch==str.charAt(i)){ //check frequency of a character
frequency++;
}
i++;
}while(iString str, char ch;
Declare and initialize integer variables as frequency=0;
The user asked to enter a string
The given string is stored in the variable str;
The user asked to enter a character
The given character is stored in the variable ch;
A loop(for, while and do-while) is used to count frequency of the character in the given string.
It is initialized as i=0, checks the condition whether i and executes the loop until the given condition becomes true
Use an if condition to test if(ch==str.charAt(i)), when the if statements is true, The frequency becomes frequency + 1( frequency = frequency +1);
When it is false, terminates the loop
Finally, the program displays the frequency of the given character of the given string
Suggested for you
for loop in Java language
while loop in Java language
do-while loop in Java language
Similar post
C++ program to count the total number of characters in the given string
C program to count the total number of characters in the given string
Python program to count the total number of characters in the given string
Java program to count the total number of upper case lower case numeric and special character
C program to count the total number of upper case lower case numeric and special character
Python program to count the total number of upper case lower case numeric and special character
C++ program to count the total number of upper case lower case numeric and special character