تحديات برمجيةالتحدي الثاني - حل التمرين الرابع بلغة C#
المطلوب
قم بإنشاء برنامج يطلب من المستخدم إدخال ثلاث نصوص و يخزنهم في ثلاث متغيرات نصية هي S1, S2 و S3 و بعدها ينفذ التالي:
- يخبره ما إن كان دمج
S1معS2يساويS3أم لا. - يخبره ما إن كان
S1يمثل جزء منS2أو يساويه ( أيS1 == s2). - إذا كان طول
S1أكبر من طولS2قم بإضافة نصS2علىS1و خزن الناتج في متغير نصي جديد إسمهS4. - إذا كان طول
S2أكبر من طولS1قم بإضافة نصS1علىS2و خزن الناتج في متغير نصي جديد إسمهS4. - يخبره ما إن كان
S2يمثل جزء من ثاني نصف فيS1. - يعرض له الأحرف الموجودة في أول نصف في
S1.
الحل بلغة C#
using System; class Program { static void Main(string[] args) { string S1, S2, S3, S4=""; Console.Write("Enter S1: "); S1 = Console.ReadLine(); Console.Write("Enter S2: "); S2 = Console.ReadLine(); Console.Write("Enter S3: "); S3 = Console.ReadLine(); Console.WriteLine("---------------------------------------"); // Part 1 Console.WriteLine("The concatenation of S1 and S2 is equal S3? " + (S3.Equals(S1 + S2))); // Part 2 Console.WriteLine("S1 is part of S2 or S1=S2? " + S2.Contains(S1)); // Part 3 if (S1.Length > S2.Length) { S4 = S2 + S1; } // Part 4 if (S2.Length > S1.Length) { S4 = S1 + S2; } // Part 5 Console.WriteLine("S2 is part of the second half of S1? " + (S1.IndexOf(S2, S1.Length / 2) != -1)); // Part 6 Console.Write("The first half characters of S1 are: "); for (int i = 0; i < S1.Length / 2; i++) { Console.Write(S1[i]); } Console.ReadKey(); } }
سنحصل على النتيجة التالية في حال تم إدخال نفس القيم التي تم تعليمها باللون الأصفر عند التشغيل.
Enter S1: Mhamad
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? True
S1 is part of S2 or S1=S2? False
S2 is part of the second half of S1? False
The first half characters of S1 are: Mha
Enter S2: Harmush
Enter S3: MhamadHarmush
---------------------------------------
The concatenation of S1 and S2 is equal S3? True
S1 is part of S2 or S1=S2? False
S2 is part of the second half of S1? False
The first half characters of S1 are: Mha