How to find the largest substring between two equal characters in a string with C#

1 Answer

0 votes
using System;

public class Program
{
	private static string GetLongestSubstring(string str) {
		int start = 0, end = 0, max = 0;
		int size = str.Length;

		for (int i = 0; i < size - 1; i++) {
			for (int j = i + 1; j < size; j++) {
				if (str[i] == str[j]) {
					int temp = Math.Abs(j - i - 1);
					if (temp > max) {
						max = temp;
						start = i + 1;
						end = j;
					}
				}
			}
		}
		return str.Substring(start, end - start);
	}

	public static void Main(string[] args)
	{
		string str = "zXoDc#programmingDkmq";

		string result = GetLongestSubstring(str);

		Console.Write(result);
	}
}





/*
run:
 
c#programming
 
*/

 



answered Dec 25, 2022 by avibootz
...