How would you improve it?

This is a discussion on How would you improve it? within the C Programming forums, part of the General Programming Boards category; The code below searches for the first instance of string "b" inside of string "a." char* stringmatch( char* a, const ...

  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
    Registered User whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    7,522
    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,261
    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をこわせ! Elysia's Avatar
    Join Date
    Oct 2007
    Posts
    22,167
    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.
    For information on how to enable C++11 on your compiler, look here.
    よく聞くがいい!私は天才だからね! ^_^

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, 04: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

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