Pseudo-code – Word Search

Leave a comment

February 8, 2013 by Ozgur Ozden

Word Search”: Given a string of letters, identify all substrings that create one of five given words. For example, if the words (arguments) are: structure; such; system; blue; red, then the string jkdistructuredstrusyssystemoon contains the first, third and fifth words, once each.
SOLUTION:
1-      Algorithm WordSearch (T,P)
2-      input: pattern P of length n and text T of length m
3-      k ← 0   Counter set to zero
4-      for i = 0 to (m – n)   \increment starts for Text
5-           for j = 0 to (n – 1)   \increment starts for Pattern
6-                if P[ j ] = T[ i +j ] then   \matched character
7-                k ← k+1
8-                    if  k = n then     \ matched pattern
9-                    Print i                \  and break the inner loop
10-           next j
11-          else
12-           k ← 0
13-           next i

Advertisements

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

%d bloggers like this: