-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathBruteForceStringSearch.cs
More file actions
34 lines (30 loc) · 919 Bytes
/
BruteForceStringSearch.cs
File metadata and controls
34 lines (30 loc) · 919 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
namespace DA.Algorithms.Strings
{
public static class BruteForceStringSearch
{
public static int BruteForceSearch (string text, string pattern)
{
return BruteForceSearch (text.ToCharArray (), pattern.ToCharArray ());
}
private static int BruteForceSearch (char[] text, char[] pattern)
{
int index = 0;
int charCounter = 0;
while (index <= (text.Length - pattern.Length))
{
charCounter = 0;
while (charCounter < pattern.Length
&& pattern[charCounter] == text[index + charCounter])
{
charCounter++;
}
if (charCounter == pattern.Length)
{
return index;
}
index++;
}
return -1;
}
}
}