In this article, we will discuss the concept of the Java 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
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; i<str.length(); i++){ if(ch==str.charAt(i)){ //check frequancy of the given character frequency++; } } System.out.println("Frequency of "+ch+" = "+frequency); // display frequency of the given character } }
When the above code is executed, it produces the following result
Enter the string Java language Enter the character a Frequency of a=4
The program allows the user to enter a String and a character then it finds the frequency of the given character in the string using while loop in Java programing language
program 2
import 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(i<str.length()){ if(ch==str.charAt(i)){ //check frequency of a character frequency++; } i++; } System.out.println("Frequency of "+ch+" = "+frequency); }// display frequency of a character }
When the above code is executed, it produces the following result
Enter the string Java programming language Enter the character g Frequency of g=4
The program allows the user to enter a String and a character and then it finds the frequency of the given character in a string using do-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(i<str.length()); System.out.println("Frequency of "+ch+" = "+frequency); }//display frequency of the given character }
When the above code is executed, it produces the following result
Enter the string code for coding Enter the character o Frequency of o=3
Approaches
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
C# inverted full pyramid star pattern In this article, we will discuss the concept of…
C# Full Pyramid star pattern program In this article, we will discuss the concept of…
Program to count vowels, consonants, words, characters and space in Java In this article, we…
How to print multiplication table using Array in C++ language In this post, we will…
C Program to multiplication table using Array In this tutorial , we will discuss about…
Java program to check odd or even using recursion In this tutorial, we discuss a…