Advertisement
StreetKatya

BruteForce

May 1st, 2023
1,021
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C# 0.97 KB | None | 0 0
  1. public class BruteForce : ISubstringSearch
  2.     {
  3.         public BruteForce()
  4.         {
  5.         }
  6.         public List<int> Search(string fileName, string searchString)
  7.         {
  8.             List<int> result = new List<int>();
  9.             int M = searchString.Length;
  10.             using (StreamReader sr = new StreamReader(fileName))
  11.             {
  12.                 string line = sr.ReadLine();
  13.                 int N = line.Length;
  14.                 for (int i = 0; i < N-M; i++)
  15.                 {
  16.                     for (int j = 0; j < M; j++)
  17.                     {
  18.                         if (line[i + j] == searchString[j])
  19.                         {
  20.                             if(j == M - 1)
  21.                             {
  22.                                 result.Add(i);
  23.                             }
  24.                         }
  25.                         else { break; }
  26.                     }
  27.                 }
  28.             }
  29.             return result;
  30.         }
  31.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement