Check whether a string is contained in another string without the string library. h?

Asked

Viewed 547 times

0

I need to check if a string is contained in another and returns the position at which the first letter appeared.

I saw here on the site the following code, but I can not use the library string.h.

include stdio.h  
include string.h

int main()   
{  
    char s1[20];  
    char s2[20];  
    printf("Digite uma string : ");  
    scanf("%s", s1);  
    printf("Digite outra string : ");  
    scanf("%s", s2);  
    printf("%ld", strstr(s1, s2) - s1);  
}  

I tried to do it this way:

void str_conteudo( char str1[], char str2[])
{  
int i=0;
int y;

while(str1[i] == str2[i] && str2[i] != 0) 
{
    if (str2[i] != str1[i])
        return i;
    i++;
}

y=i;
printf("Indice do primeiro caractere que contem a string 2: %d ", y);

}

But it’s not increasing, which may be wrong?

  • Just a few notes: 1) if you select the code and click the button { } (or use Ctrl + K), your code will be formatted; 2) [en.so] is not a forum.

  • The problem itself is clear, but you’ve tried something?

  • @LINQ tried here but it didn’t work, I edit the code and put mine then?

  • Yeah, you can [Dit] the question and just put down your attempt explaining what logic you used to get into the code and what was the problem you faced.

  • 1

    https://opensource.apple.com/source/tcl/tcl-10/tcl/compat/strstr.c.auto.html

  • @Is it normal to use that "pattern" that is being used in the parameters? I mean, to declare the types below and such. I’ve never seen that. I didn’t even know it was possible.

  • @LINQ This exists, is the original K&R form, but is considered archaic.

Show 2 more comments

1 answer

0

Just take a look at function code strstr and copy and paste it.

Disregarding the header and documentation comments, here is the code:

char *
strstr(string, substring)
    register char *string;  /* String to search. */
    char *substring;        /* Substring to try to find in string. */
{
    register char *a, *b;

    /* First scan quickly through the two strings looking for a
     * single-character match.  When it's found, then compare the
     * rest of the substring.
     */

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

There are some things that can be changed, in particular the archaic K&R notation of declaring the types of the parameters between the ) and the { be replaced by the current notation, the modifier register which can be ignored in modern compilers and the += 1 which may be replaced by ++, among other small changes possible. However, the code is essentially this and anything else you do, assuming it is correct, will be equivalent to this.

  • Sorry to be unaware, but I could not understand and I know how I would apply it in my code :/ , has functions q I do not know how to NULL, breack...

  • @lavium break serves to interrupt the while (1). The NULL indicates that the second string does not exist inside the first. continue serves to force the next iteration of for.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.