+ Reply to Thread
Results 1 to 5 of 5

Thread: String searching algorithm

  1. #1
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    String searching algorithm

    Don't read this thread. The scripts are crap and they don't work properly. I've put the fixed version here:
    http://forum.codecall.net/shell-scri...algorithm.html


    This is the first step in a regular expression parser that I'm in the process of writing. It's going to take a while, but I've got a straight string search done. The next step will be to build a Knuth-Morris-Pratt search and a Boyar-Moore search based on this one (for the purpose of speed optimization), then figure out how to parse metacharacters in the regular expression. The script takes two inputs: $1 is the substring to search for and $2 is the string to search. The output of the script is the beginning index of the substring. It goes like this:

    Code:
    #!/bin/bash
    # straight string search algorithm
    declare -i index=-1;
    for((i=0; i < ${#2}; i++))
    do
    	for((j=0; j < ${#1}; j++))
    	# loop for individual substring comparisons
    	do
    		if [ "${1:j:1}" = "${2:j+i:1}" ]
    		# index is set to j+i, for obvious reasons.
    		then
    			index=i;
    		else
    			break;
    		fi
    	done
    	if [ $index -ne -1 ]
    	then
    		break;
    		# close to the Knuth-Morris-Pratt method, only it doesn't jump ahead
    	fi
    done
    echo $index;
    unset index;
    Last edited by DarkLordoftheMonkeys; 11-12-2009 at 01:42 PM.
    Life's too short to be cool. Be a nerd.

  2. CODECALL Circuit advertisement
    Join Date
    Always
    Location
    Advertising world
    Posts
    Many

     
  3. #2
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: String searching algorithm

    Update:
    I just modified the algorithm so it does a Knuth-Morris-Pratt search instead of a straight string search. This should make it faster as it has to do fewer comparisons. I only had to add one line of code.

    Code:
    #!/bin/bash
    # straight string search algorithms
    declare -i index=-1;
    for((i=0; i < ${#2}; i++))
    do
    	for((j=0; j < ${#1}; j++))
    	# loop for individual substring comparisons
    	do
    		if [ "${1:j:1}" = "${2:j+i:1}" ]
    		# index is set to j+i, for obvious reasons.
    		then
    			index=i;
    		else
    			break;
    		fi
    	done
    	if [ $index -ne -1 ]
    	then
    		i+=j;
    		break;
    		# Knuth-Morris-Pratt method
    	fi
    done
    echo $index;
    unset index;
    Life's too short to be cool. Be a nerd.

  4. #3
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: String searching algorithm

    Here's the Boyar-Moore version of the string search. This one does it even faster.

    Code:
    #!/bin/bash
    # Boyar-Moore string search
    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
    			break;
    		fi
    	done
    	if [ $index -ne -1 ]
    	then
    		i+=${#1};
    		# skips ahead one whole substring length
    		break;
    	fi
    done
    echo $index;
    unset index;
    Life's too short to be cool. Be a nerd.

  5. #4
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: String searching algorithm

    I found a major bug in the script. I'm trying to fix it right now.
    Life's too short to be cool. Be a nerd.

  6. #5
    DarkLordoftheMonkeys's Avatar
    DarkLordoftheMonkeys is offline Programming Professional
    Join Date
    Oct 2009
    Location
    Massachussets
    Posts
    255
    Blog Entries
    56
    Rep Power
    11

    Re: String searching algorithm

    Please disregard everything in this thread. The scripts are crap. I just fixed the algorithm and I'm going to post it in another thread.
    Life's too short to be cool. Be a nerd.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. TASM Simple Reverse String Algorithm
    By assembler_wanna_be in forum Assembly
    Replies: 3
    Last Post: 12-15-2010, 01:17 AM
  2. Searching a string
    By yamman13 in forum Python
    Replies: 5
    Last Post: 06-15-2010, 04:00 PM
  3. Working string search algorithm
    By DarkLordoftheMonkeys in forum Bash / Shell Scripting
    Replies: 0
    Last Post: 11-12-2009, 09:24 AM
  4. adding int to string,creat a file named string..
    By emda321 in forum C and C++
    Replies: 5
    Last Post: 09-10-2009, 07:15 AM
  5. Algorithm for searching within ranges
    By laganojunior in forum General Programming
    Replies: 10
    Last Post: 07-25-2009, 07:08 PM

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts