المطلوب
قم بتعريف دالة إسمها PrintWordsOccurence, نمرر لها نص عند إستدعاءها فتقوم بطباعة كم مرة تكررت كل كلمة في هذا النص.
مثال: إذا قمنا باستخدام الدالة PrintWordsOccurence() و تمرير النص "I am happy. I am a doctor. I like chocolate." فإنها ستطبع النتيجة التالية.
[3] I [2] am [1] happy. [1] a [1] doctor. [1] like [1] chocolate.
الحل بلغة C#
using System;
class Program
{
static void Main(string[] args)
{
string text = "I am happy. I am a doctor. I like chocolate.";
PrintWordsOccurence(text);
Console.ReadKey();
}
static void PrintWordsOccurence(String s)
{
if (string.IsNullOrEmpty(s))
{
Console.WriteLine("There is no text!");
return;
}
string[] words = s.Split(' ');
int count;
for (int i = 0; i < words.Length; i++)
{
count = 1;
for (int j = i + 1; j < words.Length; j++)
{
if (words[i].Equals(words[j]))
{
count = count + 1;
words[j] = "";
}
}
if (!words[i].Equals(""))
{
Console.WriteLine("[" + count + "] " + words[i]);
}
}
}
}
سنحصل على النتيجة التالية عند التشغيل.
[3] I [2] am [1] happy. [1] a [1] doctor. [1] like [1] chocolate.


محرر الويب
نظام الألوان
محول الوحدات
محلل عناوين الشبكات