using System.Collections.Generic;
public static void Main()
public static void startProgram()
Console.WriteLine("Please enter the string");
Console.WriteLine("String may contain any character");
var input = Console.ReadLine();
Console.WriteLine("String length: " + input.Length);
Console.WriteLine("Please enter the pattern length");
Console.WriteLine("Pattern length should be great than 1 and shorter than length of string you entred");
var patternLength = Console.ReadLine();
Console.WriteLine("=============================");
Console.WriteLine("Please enter the valid values");
Console.WriteLine("=============================");
if (input == "" || input.Length <= Convert.ToInt32(patternLength) || Convert.ToInt32(patternLength) <= 1)
Console.WriteLine("You entered: ");
Console.WriteLine("String: " + input);
Console.WriteLine("Pattern Length: " + patternLength);
Console.WriteLine("=============================");
Console.WriteLine("Please enter the valid values");
Console.WriteLine("=============================");
usingDictionary(input, patternLength);
static void usingDictionary(string input, string patternLengthStr)
int patternLength = Convert.ToInt32(patternLengthStr);
var subStringDictionary = new Dictionary<string, int>();
bool repeatingPatternOccured = false;
for (int i = 0; i < input.Length + 1 - patternLength; i++)
for (int j = 0; j < patternLength; j++)
subString = subString + input[i + j];
if (subStringDictionary.ContainsKey(subString))
int count = subStringDictionary[subString];
subStringDictionary.Remove(subString);
subStringDictionary.Add(subString, count + 1);
subStringDictionary.Add(subString, 1);
Console.WriteLine("=============================");
Console.WriteLine("Using dictionary");
Console.WriteLine("=============================");
foreach (var keyval in subStringDictionary)
repeatingPatternOccured = true;
Console.WriteLine("Repeated pattern: " + keyval.Key);
Console.WriteLine("Occurance value: " + keyval.Value);
if (repeatingPatternOccured == false)
Console.WriteLine("There is no repeating pattern present");