Thread: finding substring

  1. #1
    Registered User
    Join Date
    Mar 2009
    Posts
    46

    Question finding substring

    Hello everyone;

    Whats the problem in following code for searching a substring in a string. The code is here-

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void sarch( char *, int , char *, int );
    void main()
    {
    int i;
    char *str="thegreathero";
    char *pattern="hero";
    int m,n;
    search(char *str,int m, char* pattern, int n);
    printf("\n \n");
    
    }
    
    void search( char *x, int m, char *y, int n)
    {
    int i,j;
    for(j=0;j<(n-m);++j)
    {
    for(i=0; i<m && x[i]=y[i+j]; ++i);
    if(i>=m){
    printf("\n match found at \n \n->[%d]\n->[%s]\n",j,y+j);
    }
    }
    Last edited by rits; 03-11-2009 at 11:03 AM.

  2. #2
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    There are a number of problems, though it would be helpful if you posted what kind of errors you got and where. Compiler/Linker/Run time?
    In any case, you don't need to specify the types of the arguments when you call the search function inside main. Because you have already declared the function at the top, the compiler knows what kind of arguments you are passing to the function. Also, you never initialise m or n, so what you pass into search could be anything from 0 to the maximum value an int can hold, it's entirely random.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  3. #3
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Yes, silly mistake inialization of m and n are missing. But one more thing i am getting compilation time error in the for loop in search function (for(i=0; i<m && x[i]=y[i+j]; ++i);
    saying l value required. Please help me out....

  4. #4
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Before I do that, indent your code properly and post the latest version of it. That way we're all reading from the same page.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  5. #5
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Here it is-

    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void search( char *, int , char *, int );
    void main()
    {
         int i;
         char *str="thegreathero";
         char *pattern="hero";
         int m=4,n=12;
         search(str, m,  pattern,  n);
         printf("\n \n");
    
    }
    
    void search( char *x, int m, char *y, int n)
    {
         int i,j;
         for(j=0;j<(n-m);++j)
        {
           for(i=0; i<m && x[i]=y[i+j]; ++i);
            if(i>=m){
            printf("\n match found at \n \n->[%d]\n->[%s]\n",j,y+j);
          }
       }
    }

  6. #6
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Code:
    for(i=0; i<m && x[i]=y[i+j]; ++i);
    this will store the value of y[i + j] into x[i] every time the loop runs

    If you want it to check if x[i] has the same value of y[i+j] you need to use == like this
    Code:
    for(i=0; i<m && x[i]==y[i+j]; ++i);
    also you terminated the for loop with a semicolon and you didn't use opening/closing curly braces (I don't think either of those are valid)

    Code:
          for(i=0; i<m && x[i]=y[i+j]; ++i);
            if(i>=m){
            printf("\n match found at \n \n->[%d]\n->[%s]\n",j,y+j);
          }
    I think this is what the correct version would look like

    Code:
               for(i=0; i<m && x[i]==y[i+j]; ++i) {
                 if(i>=m) {
                   printf("\n match found at \n \n->[%d]\n->[%s]\n",j,y+j);
                 }
               }

    is there any particular reason you are using ++i instead of i++?
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  7. #7
    Lurking whiteflags's Avatar
    Join Date
    Apr 2006
    Location
    United States
    Posts
    9,612
    >> is there any particular reason you are using ++i instead of i++?
    You say that as if it would be wrong, it isn't.

  8. #8
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    Quote Originally Posted by whiteflags View Post
    >> is there any particular reason you are using ++i instead of i++?
    You say that as if it would be wrong, it isn't.
    yeah, I just never see anyone else do that
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  9. #9
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Yes i made the changes its compiling sucessfully but damn thing is not showing any result.. is the mistake in passing m and n i have inialize these with my string and substring length.

  10. #10
    Banned ಠ_ಠ's Avatar
    Join Date
    Mar 2009
    Posts
    687
    post updated code
    ╔╗╔══╦╗
    ║║║╔╗║║
    ║╚╣╚╝║╚╗
    ╚═╩══╩═╝

  11. #11
    Technical Lead QuantumPete's Avatar
    Join Date
    Aug 2007
    Location
    London, UK
    Posts
    894
    Quote Originally Posted by ಠ_ಠ View Post
    yeah, I just never see anyone else do that
    It's ever so slightly faster than i++, because the register only has to be loaded once. In practice and on today's computers it makes very little difference.
    Also, you don't *need* braces for a for loop, as long as the looped block is only one line (or in this case an if statement with one line). However, it's good practice to *always* use braces, in case you want to add debug print statements later on.

    QuantumPete
    "No-one else has reported this problem, you're either crazy or a liar" - Dogbert Technical Support
    "Have you tried turning it off and on again?" - The IT Crowd

  12. #12
    Hurry Slowly vart's Avatar
    Join Date
    Oct 2006
    Location
    Rishon LeZion, Israel
    Posts
    6,788
    Quote Originally Posted by ಠ_ಠ View Post
    yeah, I just never see anyone else do that
    If you read more C++ code you should see it more often. For simple types (used in C) it makes no difference.

    In C++ - for types having complex copy-constructor - It could affect performance. So C++ programmers just have a habbit to prefer prefix over postfix increment decrement in places where there is no logical difference to using one over another
    All problems in computer science can be solved by another level of indirection,
    except for the problem of too many layers of indirection.
    – David J. Wheeler

  13. #13
    Registered User
    Join Date
    Mar 2009
    Posts
    46
    Here is the latest code-
    Code:
    #include<stdio.h>
    #include<stdlib.h>
    void search( char *, int , char *, int );
    void main()
    {
        int i;
        char *str="thegreathero";
        char *pattern="hero";
        int m=4,n=12;
        search(str, m,  pattern,  n);
        printf("\n \n");
      
      }
      
    void search( char *x, int m, char *y, int n)
    {
        int i,j;
        for(j=0;j<(n-m);++j)
        {
          for(i=0; i<m && x[i]==y[i+j]; i++){
          if(i>=m){
                          printf("\n match found at \n \n->[%d]\n->[%s]\n",j,y+j);
                       }
                }
         }
    }
    

  14. #14
    Frequently Quite Prolix dwks's Avatar
    Join Date
    Apr 2005
    Location
    Canada
    Posts
    8,057
    Code:
          for(i=0; i<m && x[i]==y[i+j]; i++){
          if(i>=m){
    Do you think it will ever be the case that i >= m when part of your for loop condition is i < m?

    Also: cpwiki.sf.net/Void_main
    dwk

    Seek and ye shall find. quaere et invenies.

    "Simplicity does not precede complexity, but follows it." -- Alan Perlis
    "Testing can only prove the presence of bugs, not their absence." -- Edsger Dijkstra
    "The only real mistake is the one from which we learn nothing." -- John Powell


    Other boards: DaniWeb, TPS
    Unofficial Wiki FAQ: cpwiki.sf.net

    My website: http://dwks.theprogrammingsite.com/
    Projects: codeform, xuni, atlantis, nort, etc.

  15. #15
    Registered User
    Join Date
    Oct 2008
    Location
    TX
    Posts
    2,059
    Similar thread here that you may wanna check it out.

Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Substring finding ideas
    By adrian2009 in forum C Programming
    Replies: 19
    Last Post: 03-07-2009, 03:33 PM
  2. split string and remove substring
    By nyc_680 in forum C Programming
    Replies: 3
    Last Post: 03-02-2009, 04:45 AM
  3. I need help with creating a substring program
    By CProgramingBegg in forum C Programming
    Replies: 9
    Last Post: 02-06-2009, 09:50 AM
  4. Replacing a substring with another
    By Dan17 in forum C Programming
    Replies: 3
    Last Post: 09-14-2006, 10:37 AM
  5. MFC :: Finding Child Window of a CWnd* Object?
    By SyntaxBubble in forum Windows Programming
    Replies: 2
    Last Post: 09-06-2003, 09:06 AM