Yahoo Answers is shutting down on May 4th, 2021 (Eastern Time) and beginning April 20th, 2021 (Eastern Time) the Yahoo Answers website will be in read-only mode. There will be no changes to other Yahoo properties or services, or your Yahoo account. You can find more information about the Yahoo Answers shutdown and how to download your data on this help page.

C programming substring function help?

In my lab i was told to impelement a function

substring(char * substring,char*string){

}

function's property

Enter substring:def

Enter the string:abcdefdelmde

def is substring of abcdefdelmde

Can anyone do this for me plz? to implement it

I

2 Answers

Relevance
  • cja
    Lv 7
    1 decade ago
    Favorite Answer

    I'm assuming you don't want to use strstr from string.h, because searching for a substring is exactly what that function does.

    You should really try to code this yourself; it's a good exercise that gives you a chance to work with pointers and puts your logic skills to the test. Give it a try, and compare what you have to what I did below. At the very least, study and understand my solution; I'm sure you can learn something doing that.

    #include <stdio.h>

    #include <string.h>

    #define MAX_LINE_LEN 80

    typedef enum { false = 0, true } bool;

    char *getStr(const char *, char *, size_t);

    char *substring(char *substring, char *string);

    char s1[MAX_LINE_LEN];

    char s2[MAX_LINE_LEN];

    int main(int argc, char *argv[]) {

        char *p;

        while (1) {

            getStr("\nEnter string :",s1,MAX_LINE_LEN);

            getStr("Enter substring to find :",s2,MAX_LINE_LEN);

            if ((strlen(s1) > 0) && (strlen(s2) > 0)) {

                printf("%s is",s2);

                if ((p = substring(s2,s1)) == NULL) {

                    printf(" not");

                }

                printf(" a substring of %s",s1);

                if (p != NULL) {

                    printf(", found at index %d",p-s1);

                }

                printf("\n");

            }

        }

        return 0;

    }

    char *substring(char *substring, char *string) {

        char *p = NULL, *q = string, *z = substring;

        if ((z != NULL) && (q != NULL)) {

            while ((*q != '\0') && (*z != *q)) {

                q++;

            }

            if (*q != '\0') {

                p = q;

            }

            while ((*q != '\0') && (*z != '\0') && (*q == *z)) {

                q++; z++;

            }

            if (*z != '\0') p = NULL;

        }

        return p;

    }

    char *getStr(const char *prompt, char *str, size_t len) {

        char *p;

        printf("%s ",prompt);

        if ((p = fgets(str,len,stdin)) != NULL) {

            *(strchr(str,'\n')) = '\0';

        }

        return p;

    }

    #if 0

    Sample run:

    Enter string : abcd

    Enter substring to find : ab

    ab is a substring of abcd, found at index 0

    Enter string : abcd

    Enter substring to find : cde

    cde is not a substring of abcd

    Enter string : abcd

    Enter substring to find : cd

    cd is a substring of abcd, found at index 2

    Enter string : abcd

    Enter substring to find : d

    d is a substring of abcd, found at index 3

    Enter string : abcd

    Enter substring to find : xyz

    xyz is not a substring of abcd

    Enter string : abc

    Enter substring to find : bcde

    bcde is not a substring of abc

    #endif

  • 5 years ago

    can use strstr() functionality of <string.h> header document occasion: const char *largestring = "Foo Bar Baz"; const char *smallstring = "Bar"; char *ptr; ptr = strstr(largestring, smallstring); right here, ptr would have the index of the commencing incidence of smallstring in largestring. it is case soft. In case it isn't any longer present it is going to return null in case you do no longer decide for to apply the functionality then can write your man or woman code additionally; steps would be: a million. locate the dimensions of largestring and smallstring 2. run the outer loop from 0 until at last length-a million of largestring 3. start up matching the 1st character of smallstring with each character of the largestring. 4. everywhere the 1st experience happens, run a loop from a million to length-a million of smallstring 5. save checking each character of smallstring with characters of largestring from the place the experience became stumbled on 6. If everywhere the experience is fake set flag=fake and smash the internal loop else proceed 7. whilst the internal loop completes or breaks, verify flag. no rely if it is fake proceed the outer loop else print that' experience became stumbled on" and smash the outer loop additionally

Still have questions? Get your answers by asking now.