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

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

المطلوب

قم بإنشاء برنامج يطلب من المستخدم إدخال نصّين, بعدها يقوم بطباعة ما إن كان النص الأول يبدأ بالنص الثاني أم لا.

مثال

Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'
	


الحل بلغة C#

using System;
class Program
{
static void Main(string[] args)
{
string text1, text2;
bool found = true;
Console.Write("Enter text 1: ");
text1 = Console.ReadLine();
Console.Write("Enter text 2: ");
text2 = Console.ReadLine();
for (int i = 0; i < text2.Length && found == true; i++)
{
if (text1[i] != text2[i])
{
found = false;
}
}
Console.WriteLine("--------------------------------");
if (found)
{
Console.WriteLine("Text 1 start with '" + text2 + "'");
}
else
{
Console.WriteLine("Text 1 is not start with '" + text2 + "'");
}
Console.ReadKey();
}
}
using System; class Program { static void Main(string[] args) { string text1, text2; bool found = true; Console.Write("Enter text 1: "); text1 = Console.ReadLine(); Console.Write("Enter text 2: "); text2 = Console.ReadLine(); for (int i = 0; i < text2.Length && found == true; i++) { if (text1[i] != text2[i]) { found = false; } } Console.WriteLine("--------------------------------"); if (found) { Console.WriteLine("Text 1 start with '" + text2 + "'"); } else { Console.WriteLine("Text 1 is not start with '" + text2 + "'"); } Console.ReadKey(); } }

سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.

Enter text 1: I like programming.
Enter text 2: Hello
--------------------------------
Text 1 is not start with 'Hello'