progrm to find a substring in a string

Discussion in 'C Programming' started by Ajay, Mar 22, 2006.

  1. Ajay

    Ajay Guest

    Hi all,Could anybody tell me the most efficient method to find a substr
    in a string.
    Ajay, Mar 22, 2006
    #1
    1. Advertising

  2. Ajay <> wrote:
    > Hi all,Could anybody tell me the most efficient method to find a substr
    > in a string.


    By eye.
    russell kym horsell, Mar 22, 2006
    #2
    1. Advertising

  3. Ajay wrote:
    > Hi all,Could anybody tell me the most efficient method to find a substr
    > in a string.


    That depends on what you mean by substring. If you are looking to find
    the first occurence of a particular string within another string, check
    out the standard function strstr.
    If you are looking for a replacement for the "substring" function found
    in certain other languages that returns a new string provided a given
    string, offset, and length then see my post from 3/18/2006 in the
    thread "Sub strings".

    Robert Gamble
    Robert Gamble, Mar 22, 2006
    #3
  4. Ajay

    santosh Guest

    Ajay wrote:
    > Hi all,Could anybody tell me the most efficient method to find a substr
    > in a string.


    You need to define your question better. I'm guessing that 'substr' is
    a sub-string...

    I don't know how efficient it is, but a portable way is to use the
    standard strstr() function. Look it up in a good standard library
    reference.
    santosh, Mar 22, 2006
    #4
  5. Ajay

    somu Guest

    hi ajay
    take the two string in the form of array
    compare the second with the first
    through loop
    match both
    nu will get the matching string
    that can b later taken out
    somu, Mar 22, 2006
    #5
  6. somu wrote:
    > hi ajay
    > take the two string in the form of array
    > compare the second with the first
    > through loop
    > match both
    > nu will get the matching string
    > that can b later taken out


    Without any context it's impossible to see what you're talking about.
    Your childish SMS-speak is not exactly helping either. On top of all
    that, your advice is so poorly presented that I don't think it can be
    used as a basis for any implementation.

    Read these:

    <http://cfaj.freeshell.org/google/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>

    if you want to be taken seriously and understood around here.

    --
    BR, Vladimir
    Vladimir S. Oka, Mar 22, 2006
    #6
  7. Ajay

    santosh Guest

    somu wrote:
    > hi ajay
    > take the two string in the form of array
    > compare the second with the first
    > through loop
    > match both
    > nu will get the matching string
    > that can b later taken out


    You're apparently addressing the OP while replying to my post and
    failing to include any context whatsoever, even after being requested
    to do so previously. Please read the content at the following URLs and
    heed the advice if you don't want to be ignored or plonked by most of
    the regulars in this group.

    <http://cfaj.freeshell.org/google/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    <http://en.wikipedia.org/wiki/Netiquette>
    <http://en.wikipedia.org/wiki/USENET>
    santosh, Mar 22, 2006
    #7
  8. Ajay

    code break Guest

    Ajay <> wrote:
    > Hi all,Could anybody tell me the most efficient method to find a substr
    > in a string.


    By eye.

    Be serious in usenet...
    code break, Mar 22, 2006
    #8
  9. Ajay

    Guest

    Ajay wrote:
    > Hi all,Could anybody tell me the most efficient method to find a substr
    > in a string.



    Given Code can help U................


    ********************************************************************************

    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int substring(char *,char *);

    void main()
    {
    clrscr();
    char s1[50],s2[20];
    int i,j,f=0;
    printf("Enter main string : ");
    gets(s1);
    printf("Enter substring to be searched in main string : ");
    gets(s2);
    if(substring(s1,s2)!=0)
    printf("\nFound");
    else
    printf("\nNot found");
    getch();
    }

    int substring(char *s1,char *s2)
    {
    int f=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    {
    f=1;
    s1++;
    s2++;
    }
    else
    {
    f=0;
    s1++;
    break;
    }
    }
    }
    if(f==0)
    return 0;
    else
    return 1;
    getch();
    }

    ****************************************************************************************
    Author BINNY
    , Mar 22, 2006
    #9
  10. Ajay

    Guest

    May this code help U.............
    ************************************
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int substring(char *,char *);

    void main()
    {
    clrscr();
    char s1[50],s2[20];
    int i,j,f=0;
    printf("Enter main string : ");
    gets(s1);
    printf("Enter substring to be searched in main string : ");
    gets(s2);
    if(substring(s1,s2)!=0)
    printf("\nFound");
    else
    printf("\nNot found");
    getch();
    }

    int substring(char *s1,char *s2)
    {
    int f=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    {
    f=1;
    s1++;
    s2++;
    }
    else
    {
    f=0;
    s1++;
    break;
    }
    }
    }
    if(f==0)
    return 0;
    else
    return 1;
    getch();
    }
    , Mar 22, 2006
    #10
  11. Ajay

    Guest

    May this code help U.............
    ************************************
    #include<stdio.h>
    #include<conio.h>
    #include<string.h>
    int substring(char *,char *);

    void main()
    {
    clrscr();
    char s1[50],s2[20];
    int i,j,f=0;
    printf("Enter main string : ");
    gets(s1);
    printf("Enter substring to be searched in main string : ");
    gets(s2);
    if(substring(s1,s2)!=0)
    printf("\nFound");
    else
    printf("\nNot found");
    getch();
    }

    int substring(char *s1,char *s2)
    {
    int f=0;
    for(;*s1!='\0';)
    {
    if(*s2=='\0')
    break;
    for(;*s2!='\0';)
    {
    if(*s1==*s2)
    {
    f=1;
    s1++;
    s2++;
    }
    else
    {
    f=0;
    s1++;
    break;
    }
    }
    }
    if(f==0)
    return 0;
    else
    return 1;
    getch();
    }
    , Mar 22, 2006
    #11
  12. Ajay

    santosh Guest

    wrote:
    > May this code help U.............
    > ************************************

    <snipped awful code>

    What's the point in posting the same, unreadable, non-portable code in
    three successive posts. Please quote the post to which you're replying.
    You did it for your first post, (though the quoting was woefully
    insufficient), but failed to do it for the subsequent reposts.

    Please take a moment to read the material at the follwing URLs and heed
    the advice given if you want to be considered seriously here.

    <http://cfaj.freeshell.org/google/>
    <http://clc-wiki.net/wiki/Introduction_to_comp.lang.c>
    <http://en.wikipedia.org/wiki/USENET>
    <http://en.wikipedia.org/wiki/Netiquette>
    <http://www.safalra.com/special/googlegroupsreply/>
    santosh, Mar 22, 2006
    #12
  13. wrote:
    > May this code help U.............


    I don't know if the mysterious person U will appreciate your "help".
    Some problems include:

    > #include<conio.h>

    No such header in standard C.

    > void main()

    Illegal return type for main.

    > clrscr();

    No such function in standard C.

    > char s1[50],s2[20];
    > int i,j,f=0;

    Declarations after executable statement, illegal according the C89
    standard, the one most common for extant C compilers.

    > printf("Enter main string : ");

    A prompt without a '\n' or a following fflush(stdout). There is no
    reason to think this prompt would ever be seen.

    > gets(s1);

    One of the worst errors that can be made in a C program. Never use
    gets() unless you really hate having a working computer.
    [etc. ...]
    So, tell us: are you a troll or just a very, very bad programmer?
    Martin Ambuhl, Mar 22, 2006
    #13
  14. Ajay

    santosh Guest

    wrote:
    > Ajay wrote:
    > > Hi all,Could anybody tell me the most efficient method to find a substr
    > > in a string.

    >
    > Given Code can help U................


    This group deals only with the C language as defined by it's
    international standards. Your code is quite non-standard and
    implementation specific and hence is not topical here.

    > #include<stdio.h>
    > #include<conio.h>
    > #include<string.h>
    > int substring(char *,char *);
    >
    > void main()


    Ilegal form for main(). Use either int main(void) or int main(int argc,
    char **argv).

    > {
    > clrscr();


    Implementation specific function.

    > char s1[50],s2[20];
    > int i,j,f=0;
    > printf("Enter main string : ");
    > gets(s1);


    Use of dangerous function gets(). Have you travelled in a time machine
    from the 80s? Use fgets() as a standard alternative or ggets() by
    CBFalconer as a non-standard but more robust variant.

    > printf("Enter substring to be searched in main string : ");
    > gets(s2);


    Same as above.

    > if(substring(s1,s2)!=0)
    > printf("\nFound");
    > else
    > printf("\nNot found");
    > getch();


    Non-standard function. getchar() is better.

    > }
    >
    > int substring(char *s1,char *s2)
    > {
    > int f=0;
    > for(;*s1!='\0';)
    > {
    > if(*s2=='\0')
    > break;
    > for(;*s2!='\0';)
    > {
    > if(*s1==*s2)
    > {
    > f=1;
    > s1++;
    > s2++;
    > }
    > else
    > {
    > f=0;
    > s1++;
    > break;
    > }
    > }
    > }
    > if(f==0)
    > return 0;
    > else
    > return 1;
    > getch();
    > }


    This function can be replaced by the standard strstr() function.
    santosh, Mar 22, 2006
    #14
  15. Ajay

    Flash Gordon Guest

    wrote:

    Please provide context. Google is not Usenet just an interface to it and
    there is no guarantee that other people have seen the artical you are
    replying to. See http://cfaj.freeshell.org/google/ for information on
    how to provide context properly.

    Also, please try to avoid multipl postings, although Google could well
    be to blame for this.

    > May this code help U.............


    Please avoid contractions like "U" for you, they make it much harder to
    read your post and many more people will read it than write it.

    > ************************************
    > #include<stdio.h>


    Horizontal space if very cheap these days and makes it much easier to read.
    #include <stdio.h>

    > #include<conio.h>


    conio.h is a non-standard header and we only deal with standard C here.

    > #include<string.h>
    > int substring(char *,char *);
    >
    > void main()


    main returns an int, not void. See http://c-faq.com/decl/main.html and
    related questions.

    > {
    > clrscr();


    Non standard function. See
    http://dspace.dial.pipex.com/town/green/gfd34/art/software.html for
    another possible result of using it.

    > char s1[50],s2[20];
    > int i,j,f=0;
    > printf("Enter main string : ");


    This prompt might not be displayed before the the program waits for
    input. You should use fflush(stdout); to flush the output to the display.

    > gets(s1);


    NEVER use gets. Not ever. See http://c-faq.com/stdio/getsvsfgets.html

    > printf("Enter substring to be searched in main string : ");
    > gets(s2);
    > if(substring(s1,s2)!=0)
    > printf("\nFound");
    > else
    > printf("\nNot found");
    > getch();


    getch is not a standard function. Why not use the standard getchar?

    main should return an int, so so return one.
    return 0;

    > }
    >
    > int substring(char *s1,char *s2)


    You are not modifying either string, so the following would be better
    (also in your prototype)
    int substring(const char *s1, const char *s2)

    > {
    > int f=0;
    > for(;*s1!='\0';)


    If you are not providing initialisation or increment statement, why not
    use a while? Also, why make one condition a seperate if?
    while (*s1!='\0' && *s2!='\0')

    > {
    > if(*s2=='\0')
    > break;
    > for(;*s2!='\0';)


    Again, using a for loop is stupid.
    while (*s2!='\0)

    > {
    > if(*s1==*s2)
    > {
    > f=1;
    > s1++;
    > s2++;
    > }
    > else
    > {
    > f=0;
    > s1++;
    > break;
    > }
    > }
    > }
    > if(f==0)
    > return 0;
    > else
    > return 1;


    Why not just return f?

    > getch();


    Pointless since the code can never reach here.

    > }


    I've not checked to see if the logic of your solution is sensible, there
    were enough errors without reading that closely.
    --
    Flash Gordon, living in interesting times.
    Web site - http://home.flash-gordon.me.uk/
    comp.lang.c posting guidelines and intro:
    http://clc-wiki.net/wiki/Intro_to_clc
    Flash Gordon, Mar 22, 2006
    #15
  16. "Martin Ambuhl" <> wrote in message
    news:h3gUf.8930$...
    > wrote:
    > > May this code help U.............

    > [etc. ...]
    > So, tell us: are you a troll or just a very, very bad programmer?


    You help the guy but then insult him?

    It's like me saying: "I really liked Chapter 18 on DSP's that you wrote
    (actually written by Jack Klein), but the rest of the book sucked. I want
    refund."


    Rod Pemberton
    Rod Pemberton, Mar 22, 2006
    #16
  17. Ajay

    Al Balmer Guest

    On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
    <> wrote:

    >
    >"Martin Ambuhl" <> wrote in message
    >news:h3gUf.8930$...
    >> wrote:
    >> > May this code help U.............

    >> [etc. ...]
    >> So, tell us: are you a troll or just a very, very bad programmer?

    >
    >You help the guy but then insult him?
    >

    Count the quote marks.

    --
    Al Balmer
    Sun City, AZ
    Al Balmer, Mar 22, 2006
    #17
  18. "Al Balmer" <> wrote in message
    news:...
    > On Wed, 22 Mar 2006 14:06:49 -0500, "Rod Pemberton"
    > <> wrote:
    >
    > >
    > >"Martin Ambuhl" <> wrote in message
    > >news:h3gUf.8930$...
    > >> wrote:
    > >> > May this code help U.............
    > >> [etc. ...]
    > >> So, tell us: are you a troll or just a very, very bad programmer?

    > >
    > >You help the guy but then insult him?
    > >

    > Count the quote marks.


    Ajay posted a question.
    binny.sam posted a solution.
    Ambuhl helped binny.sam & insulted binny.sam.
    I reply to Ambuhl's insult of binny.sam.
    You tell me to count quote marks... (?)

    Balmer, what the hell are you smoking out there in the desert? All posts
    seem to be available to me and are quoted as stated...


    Rod Pemberton
    Rod Pemberton, Mar 22, 2006
    #18
  19. Ajay

    Old Wolf Guest

    russell kym horsell wrote:
    > Ajay <> wrote:
    > > Hi all,Could anybody tell me the most efficient method to find a substr
    > > in a string.


    There's some (not very well structured) information at:
    http://en.wikipedia.org/wiki/String_searching_algorithm

    A good place to ask would be comp.programming where they deal
    with algorithm questions.

    > By eye.


    Come on, no horselling around please.
    Old Wolf, Mar 22, 2006
    #19
  20. Ajay

    CBFalconer Guest

    wrote:
    > Ajay wrote:
    >
    >> Hi all,Could anybody tell me the most efficient method to find
    >> a substr in a string.

    >
    > Given Code can help U................
    >
    > ****************************************************************
    >
    > #include<stdio.h>
    > #include<conio.h>
    > #include<string.h>
    > int substring(char *,char *);
    >
    > void main()
    > {
    > clrscr();
    > char s1[50],s2[20];
    > int i,j,f=0;
    > printf("Enter main string : ");
    > gets(s1);
    > printf("Enter substring to be searched in main string : ");
    > gets(s2);
    > if(substring(s1,s2)!=0)
    > printf("\nFound");
    > else
    > printf("\nNot found");
    > getch();
    > }
    >
    > int substring(char *s1,char *s2)
    > {
    > int f=0;
    > for(;*s1!='\0';)
    > {
    > if(*s2=='\0')
    > break;
    > for(;*s2!='\0';)
    > {
    > if(*s1==*s2)
    > {
    > f=1;
    > s1++;
    > s2++;
    > }
    > else
    > {
    > f=0;
    > s1++;
    > break;
    > }
    > }
    > }
    > if(f==0)
    > return 0;
    > else
    > return 1;
    > getch();
    > }


    Apart from the atrocious indentation and poor algorithm, use of the
    non-std <conio.h>, and the non-std clrscr() and getch(), and the
    failure to return exit status from main:

    junk.c:7: warning: return type of `main' is not `int'
    junk.c: In function `main':
    junk.c:8: warning: implicit declaration of function `clrscr'
    junk.c:9: warning: ISO C89 forbids mixed declarations and code
    junk.c:19: warning: implicit declaration of function `getch'
    junk.c:10: warning: unused variable `i'
    junk.c:10: warning: unused variable `j'
    junk.c:10: warning: unused variable `f'

    Also, Mr U did not request any such. Ajay did ask for an efficient
    method, without defining efficient.

    There are basically two approaches, known as Knuth Morris Pratt and
    Boyer Moore. KMP is especially attractive when input is from a
    stream, and no backtracking is allowed. BM is potentially a good
    deal more efficient. In practice the brute force algorithm, as
    exemplified by Binnys code, will often be adequate.

    Sedgewicks "Algorithms in C" discusses them all.

    --
    "If you want to post a followup via groups.google.com, don't use
    the broken "Reply" link at the bottom of the article. Click on
    "show options" at the top of the article, then click on the
    "Reply" at the bottom of the article headers." - Keith Thompson
    More details at: <http://cfaj.freeshell.org/google/>
    Also see <http://www.safalra.com/special/googlegroupsreply/>
    CBFalconer, Mar 23, 2006
    #20
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. juli
    Replies:
    3
    Views:
    8,848
    Shiva
    Dec 6, 2004
  2. Replies:
    26
    Views:
    1,607
    CBFalconer
    Jan 28, 2008
  3. Replies:
    1
    Views:
    437
    Terry Reedy
    Jun 22, 2008
  4. fishfry

    Find location of substring in string?

    fishfry, Sep 24, 2004, in forum: Perl Misc
    Replies:
    3
    Views:
    104
    Bart Lateur
    Sep 24, 2004
  5. Replies:
    3
    Views:
    189
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page