Q:

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

0

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:

All Answers

need an explanation for this answer? contact us directly to get an explanation for this answer

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 "";
        }
    }
}

need an explanation for this answer? contact us directly to get an explanation for this answer

total answers (1)

Similar questions


need a help?


find thousands of online teachers now