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

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

المطلوب

قم بتعريف دالة إسمها RemoveAll, عند استدعاءها نمرر لها نصيّن, فترجع نسخة من النص لا تحتوي على النص الثاني.
بعدها قم بتجربة هذه الدالة في البرنامج.

مثال: إذا قمنا باستخدام الدالة RemoveAll() و تمرير النص "I like cats. I have one cat." و الكلمة "cat" لها فإنها سترجع النص "I like s. I have one .".


الحل بلغة C#

using System;
class Program
{
public static string RemoveAll(string s1, string s2)
{
int L1 = s1.Length;
int L2 = s2.Length;
string newS = "";
for (int i = 0; i < L1;)
{
if (i <= L1 - L2 && s1.Substring(i, L2).Equals(s2))
{
i += L2;
}
else
{
newS += s1[i];
i += 1;
}
}
return newS;
}
static void Main(string[] args)
{
string text = "I like cats. I have one cat.";
string keyword = "cat";
string newText = RemoveAll(text, keyword);
Console.WriteLine("Before: " + text);
Console.WriteLine("After: " + newText);
Console.ReadKey();
}
}
using System; class Program { public static string RemoveAll(string s1, string s2) { int L1 = s1.Length; int L2 = s2.Length; string newS = ""; for (int i = 0; i < L1;) { if (i <= L1 - L2 && s1.Substring(i, L2).Equals(s2)) { i += L2; } else { newS += s1[i]; i += 1; } } return newS; } static void Main(string[] args) { string text = "I like cats. I have one cat."; string keyword = "cat"; string newText = RemoveAll(text, keyword); Console.WriteLine("Before: " + text); Console.WriteLine("After: " + newText); Console.ReadKey(); } }

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

Before: I like cats. I have one cat.
After:  I like s. I have one .
		

الدورات

أدوات مساعدة

أقسام الموقع

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