Write a C# Sharp program to find the longest common ending between two given strings

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

Write a C# Sharp program to find the longest common ending between two given strings.

Expected Output:

Original strings: running  ruminating

Common ending between said two strings:ing

Original strings: thisisatest  testing123testing

Common ending between said two strings:

الأجوبة

using System;
using System.Linq;
namespace exercises
{
    class Program
    {
        static void Main(string[] args)
        {
            string str1 = "running";
            string str2 = "ruminating";
            Console.WriteLine("Original strings: " + str1 + "  " + str2);
            Console.WriteLine("\nCommon ending between said two strings:" + test(str1, str2));
            str1 = "thisisatest";
            str2 = "testing123testing";
            Console.WriteLine("\nOriginal strings: " + str1 + "  " + str2);
            Console.WriteLine("\nCommon ending between said two strings:" + test(str1, str2));
        }
        public static string test(string st1, string st2)
        {
            for (int i = 0; i < st1.Length; i++)
            {
                string end = st1.Substring(i);
                if (st2.EndsWith(end))
                {
                    return end;
                }
            }
            return "";
        }
    }
}
هل كان المحتوى مفيد؟

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

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