تحديات برمجيةالتحدي الثاني - حل التمرين الثالث بلغة C#
المطلوب
قم بتعريف دالة إسمها DoubleChars
, نمرر لها نص عند إستدعاءها فتعيد لنا نسخة من هذا النص كل حرف فيها مكرر مرتين.
مثال: إذا قمنا باستخدام الدالة DoubleChars()
و تمرير النص "Iron Man"
فإنها سترجع النص "IIrroonn MMaann"
.
الحل بلغة C#
using System;
class Program
{
public static string DoubleChars(string text)
{
string newText = "";
for (int i = 0; i < text.Length; i++)
{
newText += text[i] + "" + text[i];
}
return newText;
}
static void Main(string[] args)
{
string text = "Iron Man";
string newText = DoubleChars(text);
Console.WriteLine("Before: " + text);
Console.WriteLine("After: " + newText);
Console.ReadKey();
}
}
using System;
class Program
{
public static string DoubleChars(string text)
{
string newText = "";
for (int i = 0; i < text.Length; i++)
{
newText += text[i] + "" + text[i];
}
return newText;
}
static void Main(string[] args)
{
string text = "Iron Man";
string newText = DoubleChars(text);
Console.WriteLine("Before: " + text);
Console.WriteLine("After: " + newText);
Console.ReadKey();
}
}
سنحصل على النتيجة التالية عند التشغيل.
Before: Iron Man After: IIrroonn MMaann