Thread: How would you improve it?

  1. #1
    Registered User
    Join Date
    Jul 2010
    Posts
    2

    How would you improve it?

    The code below searches for the first instance of string "b" inside of string "a."

    char* stringmatch( char* a, const char* b )
    {
    while( *a )
    {
    char *s = a, *t = b;
    while( (*s++ == *t++) && *s && *t );
    if( *t == 0 )
    return a;
    a++;
    }
    return 0;
    }


    How would you improve it?

  2. #2
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    Here you go.

    Code:
    #include <string.h>
    const char *stringmatch ( const char *a , const char *b )
    {
        return strstr ( a , b );
    }

  3. #3
    Woof, woof! zacs7's Avatar
    Join Date
    Mar 2007
    Location
    Australia
    Posts
    3,459
    There are far more better ways, if you're trying to find a sub-string within a string...

    Knuth-Morris-Pratt algorithm - Wikipedia, the free encyclopedia
    Rabin-Karp string search algorithm - Wikipedia, the free encyclopedia
    Last edited by zacs7; 07-25-2010 at 06:22 PM.

  4. #4
    Registered User
    Join Date
    Jul 2010
    Posts
    2
    Let me look at that ... At code level I was thinking if it can be made more efficient

    Looked up strstr, it internally does the same thing

  5. #5
    Algorithm Dissector iMalc's Avatar
    Join Date
    Dec 2005
    Location
    New Zealand
    Posts
    6,318
    To get something more efficient, the main change you'd have to make is to use a different algorithm such as the Boyer-Moore algorithm. There are plenty of variations on that kind of algorithm, each specialised to certain conditions such as letter frequency and the length of the string to find etc.
    If you want it faster and the runtime library functions don't cut it, then you're going to have to do spend some time researching these algorithms.
    My homepage
    Advice: Take only as directed - If symptoms persist, please see your debugger

    Linus Torvalds: "But it clearly is the only right way. The fact that everybody else does it some other way only means that they are wrong"

  6. #6
    C++まいる!Cをこわせ!
    Join Date
    Oct 2007
    Location
    Inside my computer
    Posts
    24,654
    Quote Originally Posted by Adak View Post
    io.h certainly IS included in some modern compilers. It is no longer part of the standard for C, but it is nevertheless, included in the very latest Pelles C versions.
    Quote Originally Posted by Salem View Post
    You mean it's included as a crutch to help ancient programmers limp along without them having to relearn too much.

    Outside of your DOS world, your header file is meaningless.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. How would you improve this binary tree delete function?
    By Nazgulled in forum C Programming
    Replies: 1
    Last Post: 02-20-2010, 05:38 PM
  2. Ways to Improve This?
    By Iyouboushi in forum C# Programming
    Replies: 0
    Last Post: 10-19-2009, 10:40 AM
  3. Replies: 11
    Last Post: 03-29-2009, 12:27 PM
  4. why page based I/O can improve performance?
    By George2 in forum C Programming
    Replies: 1
    Last Post: 06-12-2006, 07:42 AM
  5. can someone double check and maybe improve my code
    By tommy69 in forum C Programming
    Replies: 23
    Last Post: 04-21-2004, 02:04 PM