string searching algorithm......help

This is a discussion on string searching algorithm......help within the C Programming forums, part of the General Programming Boards category; I can't programming this algorithm(Brute-Force Algorithm, KMP Algorithm, Boyer-Moore Algorithm) How to program this algorithm ? help me!...

  1. #1
    Unregistered
    Guest

    string searching algorithm......help

    I can't programming this algorithm(Brute-Force Algorithm, KMP Algorithm, Boyer-Moore Algorithm)
    How to program this algorithm ?
    help me!

  2. #2
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    What are you searching for, a substring or chars? That makes all the difference. If searching for chars, all you have to do is go through the array of chars checking it.
    1978 Silver Anniversary Corvette

  3. #3
    Normal vector Carlos's Avatar
    Join Date
    Sep 2001
    Location
    Budapest
    Posts
    463
    Try to implement first the simpliest method.
    Boyer-Moore can wait, and you don't need such advanced techniques until you're searching in huge files.

    The algorythm is this simple:

    Search for the first character of your string in the target.
    if found, check whether second character from the search string matches the second character from target string, and so on, for th whole length of the searched string. If all chars match, you're done, return strings index from the tartget. Else, continue the search. You can even modify the algorythm to find all occurences of the given string if needed.

    // note: this is just a draft version I typed in here quickly
    // might not compile ;), just to give you an idea

    long simpleSearch( &searchString, &target )
    {
    bool matchFlag = false;
    while(1) do
    {
    if ( searchString[0] == target[y] )
    {
    matchFlag = true;
    for (int k = 1; k < strlen(searchString); k++ )
    {
    if ( searchString[k] != target[y+k] )
    {
    matchFlag = false;
    break;
    } // if
    } // for
    if ( matchFlag ) break;
    }

    y++;
    if ( y > len( target[] ) ) // target string exceeded?
    {
    matchFlag = false;
    break;
    }
    } // while

    if ( matchFlag ) // if found,
    return (y) // return starting index

    else
    return -1; // return -1 if none found
    }

  4. #4
    the Corvetter
    Join Date
    Sep 2001
    Posts
    1,584
    Yup that's the idea. When searching, you can look for the substring like that. Although in large files, that can be quite time comsuming. But, start out with that. It's good.
    1978 Silver Anniversary Corvette

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. OOP Question DB Access Wrapper Classes
    By digioz in forum C# Programming
    Replies: 2
    Last Post: 09-07-2008, 05:30 PM
  2. C++ ini file reader problems
    By guitarist809 in forum C++ Programming
    Replies: 7
    Last Post: 09-04-2008, 07:02 AM
  3. String issues
    By The_professor in forum C++ Programming
    Replies: 7
    Last Post: 06-12-2007, 10:11 AM
  4. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 01:13 AM
  5. can anyone see anything wrong with this code
    By occ0708 in forum C++ Programming
    Replies: 6
    Last Post: 12-07-2004, 12:47 PM

1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21