تحديات برمجيةالتحدي الثالث - حل التمرين الخامس بلغة C#
المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال نصّين, بعدها يقوم بطباعة ما إن كان النص الأول ينتهي بالنص الثاني أم لا.
مثال
Enter text 1: Welcome to my world Enter text 2: world -------------------------------- Text 1 is end with 'world'
الحل بلغة C#
using System;
class Program
{
static void Main(string[] args)
{
string text1, text2;
int text2Counter = 0;
bool found = true;
Console.Write("Enter text 1: ");
text1 = Console.ReadLine();
Console.Write("Enter text 2: ");
text2 = Console.ReadLine();
for (int i = text1.Length - text2.Length; i < text1.Length && found == true; i++)
{
if (text1[i] != text2[text2Counter])
{
found = false;
}
text2Counter++;
}
Console.WriteLine("--------------------------------");
if (found)
{
Console.WriteLine("Text 1 end with '" + text2 + "'");
}
else
{
Console.WriteLine("Text 1 is not end with '" + text2 + "'");
}
Console.ReadKey();
}
}
using System;
class Program
{
static void Main(string[] args)
{
string text1, text2;
int text2Counter = 0;
bool found = true;
Console.Write("Enter text 1: ");
text1 = Console.ReadLine();
Console.Write("Enter text 2: ");
text2 = Console.ReadLine();
for (int i = text1.Length - text2.Length; i < text1.Length && found == true; i++)
{
if (text1[i] != text2[text2Counter])
{
found = false;
}
text2Counter++;
}
Console.WriteLine("--------------------------------");
if (found)
{
Console.WriteLine("Text 1 end with '" + text2 + "'");
}
else
{
Console.WriteLine("Text 1 is not end with '" + text2 + "'");
}
Console.ReadKey();
}
}
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter text 1: Welcome to my world
Enter text 2: world
--------------------------------
Text 1 is end with 'world'
Enter text 2: world
--------------------------------
Text 1 is end with 'world'