Programming Basics SQL HTML CSS JavaScript Python C++ Java JavaFX Swing Problem Solving English English Conversations Computer Fundamentals Learn Typing

تحديات برمجيةالتحدي الثالث - حل التمرين الأول بلغة C#

المطلوب

قم بتعريف دالة إسمها CountWords, عند استدعاءها نمرر لها نص, فترجع عدد الكلمات الموجودة في هذا النص.
بعدها قم بتجربة هذه الدالة في البرنامج.

مثال: إذا قمنا باستخدام الدالة CountWords() و تمرير النص "Programming is easy to learn." فإنها سترجع الرقم 5.


الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
string text = "Programming is easy to learn.";
int numberOfWords = CountWords(text);
Console.Write("Total words: " + numberOfWords);
Console.ReadKey();
}
static int CountWords(string s)
{
if (string.IsNullOrEmpty(s))
{
return 0;
}
int count = s.Split(' ').Length;
return count;
}
}
using System; class Program { static void Main(string[] args) { string text = "Programming is easy to learn."; int numberOfWords = CountWords(text); Console.Write("Total words: " + numberOfWords); Console.ReadKey(); } static int CountWords(string s) { if (string.IsNullOrEmpty(s)) { return 0; } int count = s.Split(' ').Length; return count; } }

سنحصل على النتيجة التالية عند التشغيل.

Total words: 5
		

الدورات

أدوات مساعدة

أقسام الموقع

دورات
مقالات كتب مشاريع أسئلة