C# Sharp Exercises: Find the longest common ending between two given strings
C# Sharp String: Exercise-56 with Solution
Write a C# Sharp program to find the longest common ending between two given strings.
Sample Solution:-
C# Sharp Code:
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 "";
}
}
}
Sample Output:
Original strings: running ruminating Common ending between said two strings:ing Original strings: thisisatest testing123testing Common ending between said two strings:
Flowchart :
C# Sharp Code Editor:
Improve this sample solution and post your code through Disqus
Previous: Write a C# Sharp program reverse all the words of a given string which have even length.
Next: C# Sharp programming exercises Function with Solution.
What is the difficulty level of this exercise?
Test your Programming skills with w3resource's quiz.
- New Content published on w3resource:
- HTML-CSS Practical: Exercises, Practice, Solution
- Java Regular Expression: Exercises, Practice, Solution
- Scala Programming Exercises, Practice, Solution
- Python Itertools exercises
- Python Numpy exercises
- Python GeoPy Package exercises
- Python Pandas exercises
- Python nltk exercises
- Python BeautifulSoup exercises
- Form Template
- Composer - PHP Package Manager
- PHPUnit - PHP Testing
- Laravel - PHP Framework
- Angular - JavaScript Framework
- Vue - JavaScript Framework
- Jest - JavaScript Testing Framework