Skip to content

Instantly share code, notes, and snippets.

@ajinkyakulkarni
Created September 3, 2014 02:30
Show Gist options
  • Save ajinkyakulkarni/c9ec2a00bc33af5e59fd to your computer and use it in GitHub Desktop.
Save ajinkyakulkarni/c9ec2a00bc33af5e59fd to your computer and use it in GitHub Desktop.
using System;
using System.Text;
namespace BruteForceStringMatching
{
class Program
{
static void Main(string[] args)
{
String sampleText = "Lorem sag ipsum dolsagitor sagittis amet.";
String searchPattern = "sagittis";
StringMatch(sampleText, searchPattern);
Console.Read();
}
static int StringMatch(String T, String P)
{
int n = T.Length;
int m = P.Length;
for (int i = 0; i < (n - m); i++)
{
int j = 0;
while (j < m && P[j] == T[i + j])
{
j++;
}
if (j == m)
{
Console.WriteLine("Pattern found at position " + i);
return i;
}
}
Console.WriteLine("Pattern not found");
return -1;
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment