Tech Guide Blog

Tech Guide Programming Tutorial Tips Tricks

Boyer Moore String Searching Algorithm

Posted by Admin On June - 23 - 2009

This time, we will talk about Boyer Moore string searching. It’s not like brute-force, it will search a substring from the back and go forward till the first char of string. For example, we search a substring EXAMPLE within HERE IS A SIMPLE EXAMPLE string. I can’t explain it well, so this below maybe rather confusing.

EXAMPLE
HERE IS A SIMPLE EXAMPLE
's' is not in EXAMPLE substring, jump for EXAMPLE length = 7
       EXAMPLE
HERE IS A SIMPLE EXAMPLE
'e' is not same as 'p', so jump for the last char of EXAMPLE to 'p' EXAMPLE = 2
         EXAMPLE
HERE IS A SIMPLE EXAMPLE
'a' != 'i', jump for distance from 'e' SIMPLE to the front 'e' EXAMPLE sub string = 6
               EXAMPLE
HERE IS A SIMPLE EXAMPLE
'e' is not same as 'p', so jump for the last char of EXAMPLE to 'p' EXAMPLE = 2
                 EXAMPLE
HERE IS A SIMPLE EXAMPLE
compare till end, found.

For the code, it’s a little weird, made by myself with many helps. It just find the index of string if it found the sub string within the string. You can use a table to save all chars shift value.

#include <stdio.h>
#include <string.h>
#include <conio.h>
#include <windows.h>

void main()
{
	char string[1000], substring[1000];
	int strvalue[256], i, j, diff = 0, count = 0;

	printf("Input string: ");
	gets(string);
	printf("Input substring: ");
	gets(substring);
	for (i = 0; i < 256; i++)
	{
		strvalue[i] = -1;
	}
	for (i = strlen(substring) - 1; i >= 0 ; i--)
	{
		if (strvalue[substring[i]] == -1)
			strvalue[substring[i]] = i;
	}
	for (i = 0; i < 256; i++)
	{
		if (strvalue[substring[i]] == -1)
			strvalue[substring[i]] = strlen(substring);
	}
	i = strlen(substring) - 1;
	while (true)
	{
		for (j = strlen(substring) - 1; j >= 0 ; j--)
		{
			if (substring[j] == string[i - diff])
			{
				count++;
				diff++;
			}
			else
				break;
			if (count == strlen(substring))
			{
				printf("%d", i - diff + 2);
				getch();
				exit(0);
			}
		}
		diff = strlen(substring) - strvalue[string[i - diff]] - 1 - count;
		if (diff > 0)
		{
			i = i + diff;
			diff = 0;
			count = 0;
		}
		else
			i++;
		if (i > strlen(string) + strlen(substring) + 2)
			exit(0);
	}
}

Popularity: 67% [?]

Share and Enjoy:
  • Print
  • Digg
  • Sphinn
  • del.icio.us
  • Facebook
  • Mixx
  • Google Bookmarks
  • Blogplay
  • MySpace
  • StumbleUpon
  • Suggest to Techmeme via Twitter
  • Technorati
  • Twitter

4 Responses to “Boyer Moore String Searching Algorithm”

  1. Katy says:

    Pretty nice post. I just came across your site and wanted to say
    that I have really liked reading your blog posts. Anyway
    I’ll be subscribing to your blog and I hope you write again soon!

  2. Adit says:

    Your site has become wonderfull.. I like your site design.. Maybe we can share each other to improve our skill in website environment..
    Now, i interest to write about programming into my blog.. hope it does few day..

  3. Admin says:

    @Katy: thanks a lot for your support, I will write more

    @Adit: I am using a free template anyway, I do like Wordpress because of this

  4. Danny says:

    Thanks 4 your post

    it really help me in my lab ^_^v

Leave a Reply