English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية
في هذا البرنامج، ستتعلم البحث عن عدد مرات ظهور الحرف في النص المحدد (التردد).
public class Frequency { public static void main(String[] args) { String str = \ char ch = 'e'; int frequency = 0; for(int i = 0; i < str.length(); i++) { if(ch == str.charAt(i)) { ++frequency; } } System.out.println("Frequency of " + ch + " = " + frequency); } }
When running the program, the output is:
Frequency of e = 4
In the above program, we use the string method length() to find the length of the given string str.
We use the charAt() function to loop through each character in the string, which accepts an index (i) and returns the character at the given index.
We compare each character with the given character ch. If it matches, we increase the frequency value by 1.
Finally, we get a total count of the number of occurrences stored in a character, and we print the value of frequency.