substring function

Discussion in 'C++' started by Radhika Sambamurti, Feb 20, 2004.

  1. Hi,
    I've written a substring function.
    The prototype is: int substr(char s1[], char s2[])
    Returns 1 if s2 is a substring of s1, else it returns 0.
    I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function.

    //// code follows ///////////

    //implement a substring function, where if string2 is a sub of string 1, then the value returned is 1 else, 0

    #include<iostream>
    #include<cstring>
    using namespace std;

    const int MAX = 10;
    int substring(char *str1, char *str2); //prototype

    int main()
    {
    char response = 'y';
    char str1[MAX];
    char str2[MAX];
    int result = 0;

    while(response == 'Y' || response == 'y')
    {
    cout << "Please enter string 1: " ;
    cin >> str1;

    cout << "\nPlease enter string 2: ";
    cin >> str2;

    cout << endl;

    result = substring(str1, str2); //function call

    if(result == 1)
    cout << "string " << str2 << " is a substring of " << str1 << endl;
    else
    cout << "string " << str2 << " is NOT a substring of " << str1 << endl;

    cout << "would you like to enter another? (Y/N) " << endl;
    cin >> response;

    }

    return 0;
    }

    //''''''''''''''''''''''''''''''''''''''''''
    //Function definition substring

    int substring(char *str1, char *str2)
    {
    int len1, len2, i=0, j = 0, k, n;
    len1=strlen(str1); //determine the lengths of the strings
    len2=strlen(str2);


    while(i <len1)
    {

    while( str2[j] == str1 )
    {

    for(k=1, n=i+1; k<len2; k++, n++)

    //n is assigned i, which is where the match //starts in string 1- starting at the next letter

    {
    if( str2[k] != str1[n] )
    return 0; //the match fails after having made an initial match
    }
    return 1;

    j++;
    }

    i++;
    }


    }
     
    Radhika Sambamurti, Feb 20, 2004
    #1
    1. Advertising

  2. Radhika Sambamurti

    Joe C Guest

    "Radhika Sambamurti" <> wrote in message
    news:...
    > Hi,
    > I've written a substring function.
    > The prototype is: int substr(char s1[], char s2[])
    > Returns 1 if s2 is a substring of s1, else it returns 0.
    > I have written this program, but Im sure there is an easier way to do

    this. I am hoping someone can point me to a more elegant way of writing this
    same function.
    >


    Why not use the std::string member functions?

    find( )
    find_first_of( )
    find_last_of( )
    find_first_not_of( )
    find_last_not_of( )
    rfind( )

    These return the starting position of the where the match is found, or a -1
    cast to unsigned int if no match is found.
     
    Joe C, Feb 20, 2004
    #2
    1. Advertising

  3. Radhika Sambamurti

    Julie J. Guest

    How about this:

    int substring(char *str1, char *str2)
    {
    return (strstr(str1, str2) != NULL);
    }

    -jj-

    Radhika Sambamurti wrote:
    >
    > Hi,
    > I've written a substring function.
    > The prototype is: int substr(char s1[], char s2[])
    > Returns 1 if s2 is a substring of s1, else it returns 0.
    > I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function.
    >
    > //// code follows ///////////
    >
    > //implement a substring function, where if string2 is a sub of string 1, then the value returned is 1 else, 0
    >
    > #include<iostream>
    > #include<cstring>
    > using namespace std;
    >
    > const int MAX = 10;
    > int substring(char *str1, char *str2); //prototype
    >
    > int main()
    > {
    > char response = 'y';
    > char str1[MAX];
    > char str2[MAX];
    > int result = 0;
    >
    > while(response == 'Y' || response == 'y')
    > {
    > cout << "Please enter string 1: " ;
    > cin >> str1;
    >
    > cout << "\nPlease enter string 2: ";
    > cin >> str2;
    >
    > cout << endl;
    >
    > result = substring(str1, str2); //function call
    >
    > if(result == 1)
    > cout << "string " << str2 << " is a substring of " << str1 << endl;
    > else
    > cout << "string " << str2 << " is NOT a substring of " << str1 << endl;
    >
    > cout << "would you like to enter another? (Y/N) " << endl;
    > cin >> response;
    >
    > }
    >
    > return 0;
    > }
    >
    > //''''''''''''''''''''''''''''''''''''''''''
    > //Function definition substring
    >
    > int substring(char *str1, char *str2)
    > {
    > int len1, len2, i=0, j = 0, k, n;
    > len1=strlen(str1); //determine the lengths of the strings
    > len2=strlen(str2);
    >
    >
    > while(i <len1)
    > {
    >
    > while( str2[j] == str1 )
    > {
    >
    > for(k=1, n=i+1; k<len2; k++, n++)
    >
    > //n is assigned i, which is where the match //starts in string 1- starting at the next letter
    >
    > {
    > if( str2[k] != str1[n] )
    > return 0; //the match fails after having made an initial match
    > }
    > return 1;
    >
    > j++;
    > }
    >
    > i++;
    > }
    >
    >
    > }
    >
    >
    >
    >
    >
    >
    >
    >
     
    Julie J., Feb 20, 2004
    #3
  4. Radhika Sambamurti

    Default User Guest

    Radhika Sambamurti wrote:
    >
    > Hi,
    > I've written a substring function.
    > The prototype is: int substr(char s1[], char s2[])
    > Returns 1 if s2 is a substring of s1, else it returns 0.



    Why? Is this for a school exercise, or self-teaching? The above function
    is almost, but not quite, the same as the C standard function strstr().

    Why even mess with C-style strings?

    > I have written this program, but Im sure there is an easier way to do this. I am hoping someone can point me to a more elegant way of writing this same function.


    int substr(char s1[], char s2[])
    {
    if (strstr (s1, s2))
    return 1;
    return 0;
    }

    ;)

    > int substring(char *str1, char *str2)
    > {
    > int len1, len2, i=0, j = 0, k, n;
    > len1=strlen(str1); //determine the lengths of the strings
    > len2=strlen(str2);


    These calls to strlen() are going to whack your efficiency. They
    terminate C-style strings for a reason, use that in your routine. You
    don't need to know the lengths, just the endpoints.

    >
    > while(i <len1)
    > {
    >
    > while( str2[j] == str1 )
    > {
    >
    > for(k=1, n=i+1; k<len2; k++, n++)
    >
    > //n is assigned i, which is where the match //starts in string 1- starting at the next letter
    >
    > {
    > if( str2[k] != str1[n] )
    > return 0; //the match fails after having made an initial match
    > }
    > return 1;
    >
    > j++;
    > }
    >
    > i++;
    > }
    >
    >
    > }
    >


    Here's one to consider:


    int substr(const char *string, const char *substring)
    {
    const char *a = string;
    const char *b = substring;

    b = substring;
    if (*b == 0)
    {
    return 1;
    }
    for ( ; *string != 0; string += 1)
    {
    if (*string != *b)
    {
    continue;
    }
    while (1)
    {
    if (*b == 0)
    {
    return 1;
    }
    if (*a++ != *b++)
    {
    break;
    }
    }
    }
    return 0;
    }


    Brian Rodenborn
     
    Default User, Feb 20, 2004
    #4
  5. Radhika Sambamurti

    Default User Guest

    Default User wrote:

    > int substr(const char *string, const char *substring)
    > {
    > const char *a = string;
    > const char *b = substring;
    >
    > b = substring;


    The above line is superfluous and should have been removed when I move
    the assignments up to initializers.
    Sorry.




    Brian Rodenborn
     
    Default User, Feb 20, 2004
    #5
  6. On Fri, 20 Feb 2004 22:16:39 GMT
    Default User <> wrote:

    > Radhika Sambamurti wrote:
    > >
    > > Hi,
    > > I've written a substring function.
    > > The prototype is: int substr(char s1[], char s2[])
    > > Returns 1 if s2 is a substring of s1, else it returns 0.

    >
    >
    > Why? Is this for a school exercise, or self-teaching? The above function
    > is almost, but not quite, the same as the C standard function strstr().
    >

    Thanks! Yes, this was a class exercise.
     
    Radhika Sambamurti, Feb 20, 2004
    #6
  7. Radhika Sambamurti

    Jorge Rivera Guest


    >
    > These return the starting position of the where the match is found, or a -1
    > cast to unsigned int if no match is found.


    I know that this is practically irrelevant, but the correct return value
    is std::string::npos, not -1.
     
    Jorge Rivera, Feb 21, 2004
    #7
  8. Radhika Sambamurti

    Default User Guest

    Radhika Sambamurti wrote:
    >
    > On Fri, 20 Feb 2004 22:16:39 GMT
    > Default User <> wrote:
    >
    > > Radhika Sambamurti wrote:
    > > >
    > > > Hi,
    > > > I've written a substring function.
    > > > The prototype is: int substr(char s1[], char s2[])
    > > > Returns 1 if s2 is a substring of s1, else it returns 0.

    > >
    > >
    > > Why? Is this for a school exercise, or self-teaching? The above function
    > > is almost, but not quite, the same as the C standard function strstr().
    > >

    > Thanks! Yes, this was a class exercise.


    Ok. What you had wasn't too bad, although much more appropriate for a C
    class than C++. I'm suspicious of the quality of instruction you are
    getting.




    Brian Rodenborn
     
    Default User, Feb 21, 2004
    #8
    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. Dave
    Replies:
    3
    Views:
    378
    Christopher Boomer
    Aug 6, 2003
  2. Harvey Thomas
    Replies:
    0
    Views:
    392
    Harvey Thomas
    Aug 6, 2003
  3. Dominique Deleris
    Replies:
    4
    Views:
    496
  4. Jeff

    improve my substring function?

    Jeff, May 8, 2009, in forum: C Programming
    Replies:
    2
    Views:
    572
  5. Replies:
    3
    Views:
    231
    Sherm Pendley
    Aug 3, 2005
Loading...

Share This Page