Jump to content

Working string search algorithm

- - - - -

  • Please log in to reply
No replies to this topic

#1
DarkLordoftheMonkeys

DarkLordoftheMonkeys

    Programming Professional

  • Members
  • PipPipPipPipPip
  • 255 posts
Okay, this one actually works, and it is a true replica of the Boyer-Moore algorithm:


#!/bin/bash

# Boyer-Moore string search

# inputs: $1 - substring, $2 - string

declare -i index=-1;

for((i=0; i < ${#2}; i++))

do

	for((j=${#1}-1; j >= 0; j--))

	# loop for individual substring comparisons

	do

		if [ "${1:j:1}" = "${2:i+j:1}" ]

		# compares backward

		then

			index=i;

		else

			index=-1;

			i+=(${#1}-j);

			# Boyer-Moore jump

			break;

		fi

	done

	if [ $index -ne -1 ]

	then

		break;

		# stop if substring has been found

	fi

done

echo $index;

unset index;


Sorry for messing up the last post.
Life's too short to be cool. Be a nerd.




1 user(s) are reading this topic

0 members, 1 guests, 0 anonymous users