English | 简体中文 | 繁體中文 | Русский язык | Français | Español | Português | Deutsch | 日本語 | 한국어 | Italiano | بالعربية

تعليمية Java الأساسية

تحكم في العملية Java

مجموعات Java Array

Java توجيهية للأغراض (I)

Java توجيهية للأغراض (II)

Java توجيهية للأغراض (III)

معالجة الاستثناءات Java

قائمة Java List

Java Queue (الصف)

مجموعات Java Map

مجموعات Java Set

مدخلات/مخرجات Java (I/O)

قراءات/كتابات Java

مواضيع أخرى في Java

البحث عن تردد الحرف في النص باستخدام لغة Java

Java Examples Comprehensive

في هذا البرنامج، ستتعلم البحث عن عدد مرات ظهور الحرف في النص المحدد (التردد).

مثال: البحث عن عدد مرات ظهور الحرف، التردد

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.

Java Examples Comprehensive