Searching strings - substring's

This is a discussion on Searching strings - substring's within the C Programming forums, part of the General Programming Boards category; Building a search function, I want to build a function that checks if in a string, contains an substring that ...

  1. #1
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807

    Searching strings - substring's

    Building a search function, I want to build a function that checks if in a string, contains an substring that the use define, hmm, I know that have the strstr(), but I want to build a simple one, by myself.

    I want to understand, how's exactly it works?
    Example, I have the string "Isaac" and want to check the substring "aa" for existance.

    Now, I should separete the string in all possible 2 (the size of the substring we want to search) substrings? like: is, sa, aa, ac? and than check if they are what we're searching? isn't this so many job to the pc?

    Hmm, I'm in the right way?

    Last edited by Vber; 02-05-2003 at 03:10 PM.

  2. #2
    ATH0 quzah's Avatar
    Join Date
    Oct 2001
    Posts
    14,826
    Here's a simple one:
    Code:
    char * mystrstr( const char *s1, const char *s2 )
    {
        return strstr( s1, s2 );
    }
    It doesn't get much easier than that.

    Actually, all you do is loop through the main string to find the first character in that string that matches the first character in the "to find" string.

    If it matches, check the next character in both strings. Repeat.

    If at any point it doesn't match, go back to step 1. (start looking for the first character in the "to find" string again from that point in the "look here" string).

    Quzah.
    Hope is the first step on the road to disappointment.

  3. #3
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Quzah, thanks for your help, I tried to build the function and it works:

    Code:
    #include <stdio.h>
    #include <string.h>
    
    /* the function to check if a substring is contained in a string */
    int strfind(const char *source, const char *what, const char *start);
    
    int main(void)
    {
        char *strTest = "ledicia"; /* full string */
        char *strFind = "dicis";    /* substring to find in full string */
        
        printf("The full string is: %s\n",strTest);
        printf("We'll search for the \"%s\" substring\n",strFind);
        
        if(strfind(strTest,strFind,strFind))
            printf("yes, the susbtring was found in the string\n");
        else
            printf("no, the substring wasn't found in the string\n");
        
        printf("Press any key...\n");
        getchar(); /* pause */
        return 0;
    }
    
    int strfind(const char *source, const char *what, const char *start)
    {
        
       int  corr, len = strlen(what);
         
       corr = 0;
       
       while(*source && corr <= len) {
          if(*source == *what) {
                ++source;
                ++what;
                ++corr;
          }
          
          else {
                ++source;
                what = start;
          }
       }
       
       
       if(corr == len)
          return 1;
       
       return 0;
    }
    Hmm, thanks for you explaining me a simple way to do that
    Just a simple question, when I do ++ to an pointer, he move's on the memory, but if I want to go back to the start? I used another variable, but maybe I'm missing something?

  4. #4
    End Of Line Hammer's Avatar
    Join Date
    Apr 2002
    Posts
    6,231
    >>if I want to go back to the start? I used another variable, but maybe I'm missing something?
    No, you didn't miss anything. Keeping a pointer to the start and copying its value to a "work" pointer is normal practice.

    Remember though, some of the names you've used are reserved (those starting str...)
    When all else fails, read the instructions.
    If you're posting code, use code tags: [code] /* insert code here */ [/code]

  5. #5
    Registered User Vber's Avatar
    Join Date
    Nov 2002
    Posts
    807
    Ok, Hammer, thanks for your help.
    I'll change the name of the function, this shouldn't be my problem

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Searching for a series of possible substrings inside a string
    By andrew.bolster in forum C Programming
    Replies: 7
    Last Post: 02-10-2008, 01:20 AM
  2. C++ std routines
    By siavoshkc in forum C++ Programming
    Replies: 33
    Last Post: 07-28-2006, 12:13 AM
  3. Visual C++ 2005 linking and file sizes
    By Rune Hunter in forum C++ Programming
    Replies: 2
    Last Post: 11-12-2005, 09:41 PM
  4. Searching and matching strings: segmentation fault
    By Smola in forum C Programming
    Replies: 18
    Last Post: 07-11-2005, 12:25 AM
  5. Ofstream, Ifstream, Searching files for strings?
    By Zeusbwr in forum C++ Programming
    Replies: 10
    Last Post: 04-04-2005, 03:45 PM

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