String Algorithms
Print this page

Let          Pattern P = CAT
               Text = ABABNACATMAN
Then there is a match with the shift 7 in the text T

 
Fig : 1

Brute Force String Matching algorithm.

  • This algorithm is a simple and obvious one, in which we compare a given pattern P with each of the sub strings of the text T, moving from left to right, until a match is found
  • Let Si is the substring of T, beginning at the i th position and whose length is same as pattern P.
  • We compare P, character by character, with the first substring S1. If all the corresponding characters are same, then the pattern P appears in T at shift 1. If some of the characters of S1 are not matched with he corresponding characters of P, then we try for the next substring S2. This procedure continues till the input text exhausts.
  • In this algorithm we have to compare P with n-m+1 substrings of T.
 
Prev