Write a C# Sharp program to find the middle character(s) of a given string. Return the middle character if the length of the string is odd and return two middle characters if the length of the string is even


Write a C# Sharp program to find the middle character(s) of a given string. Return the middle character if the length of the string is odd and return two middle characters if the length of the string is even. 

Expected Output :

Original string: Python
Middle character(s) of the said string: th

Original string: PHP
Middle character(s) of the said string: H

Original string: C#
Middle character(s) of the said string: C#

الأجوبة

ابحث عن مسائل برمجة سي شارب | c# programming بالانجليزي

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string text = "Python"; 
            Console.WriteLine("Original string: " + text);
            Console.WriteLine("Middle character(s) of the said string: "+test(text));
            text = "PHP";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Middle character(s) of the said string: " + test(text));
            text = "C#";
            Console.WriteLine("\nOriginal string: " + text);
            Console.WriteLine("Middle character(s) of the said string: " + test(text));
        }
        public static string test(string text)
        {
            int l = 1 - text.Length % 2;
            return text.Substring(text.Length / 2 - l, 1 + l);
        }
    }
}

محتاج مساعدة؟ تواصل مع مدرس اونلاين الان!