Write a C# Sharp program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged

  • برمجة سي شارب
  • برمجة

Write a C# Sharp program to create a new string where 'if' is added to the front of a given string. If the string already begins with 'if', return the string unchanged

Sample Output:

if else
if else

الأجوبة

using System;
namespace exercises
{
     class Program
    {
      static void Main(string[] args)
        {
            Console.WriteLine(test("if else"));
            Console.WriteLine(test("else"));
            Console.ReadLine();
        }
        
       public static string test(string s)
        {
            if (s.Length > 2 && s.Substring(0, 2).Equals("if"))
            {
                return s;
            }
            return "if " + s;
        }
  }
}
هل كان المحتوى مفيد؟

تبحث عن مدرس اونلاين؟

محتاج مساعدة باختيار المدرس الافضل؟ تواصل مع فريقنا الان لمساعدتك بتأمين افضل مدرس
ماهو التخصص الذي تبحث عنه؟
اكتب هنا...