Doubt in string, and comparison

Asked

Viewed 350 times

1

I am doubtful in the following exercise:

Write a C program that receives two strings via standard input and whether the second string is contained in the first string, i.e., if the second string is a segment of the first. You can consider, in your program, that the second string is always smaller than the first, and that the strings have at most 100 characters. Follow an example of input and output. What is underlined has been provided by the user.

  • String 1: program
  • String 2: grass

The second string is contained in the first!

The code I made always says that "The second string is contained in the first". Follows the code:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main(int argc, char**argv){
    
    char string1[101];
    char string2[101];
    int i, x;
    
    printf("Forneca a string 1:  \n");
    scanf("%s",string1);
    printf("Forneca a string 2:  \n");
    scanf("%s",string2);
    
    x=strlen(string2);
    for(i=0; i<x; i++){
        if(string2[i]=string1[i]){
            printf("A segunda string esta contida na primeira. \n");
            break;
        }
    }
    return 0;
}
  • 1

    What’s not working out?

  • to) = would be ==; b) scanf("%s",string1) only reads a word (ends in the first space...

2 answers

2

The operator =, in C, it is not a comparison, it is an assignment. In the passage you make:

string2[i]=string1[i]

You are overwriting the i-none character of string2. And yes, inside a if an assignment is true, so what is inside the if is executed.

To compare, use ==.

Another thing, you need to change your algorithm. It will say that one string is inside the other whenever the characters in the same position are equal.

Instead, do it like this:

  • Run all characters of the larger string until you find one that matches the first character of the smaller string.
  • From there, if the N-1 next characters of the larger string (where N is the size of the smaller string) are equal to the next N-1 characters of the smaller one, the smaller one is contained in the larger one.
  • If the above step is not conclusive, continue looking for another character in the larger string that is equal to the first character of the smaller string and repeat the second step.
  • If you have reached the (M - N)-th (where M is the length of the larger string, and N the size of the smaller) character of the larger string and you have not found the smaller string within it, the smaller string is not contained in the larger string.

Good luck!

  • I’ll try here, thank you very much!

1

Function strstr() standard library checks if one string is contained in the other, there is no need to reinvent the wheel:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

int main( int argc, char * argv[] )
{
    char str1[100];
    char str2[100];

    printf("Forneca a primeira string: ");
    fgets( str1, sizeof(str1), stdin );
    str1[ strcspn(str1, "\n") ] = 0;

    printf("Forneca a segunda string: ");
    fgets( str2, sizeof(str2), stdin );
    str2[ strcspn(str2, "\n") ] = 0;

    if( strstr( str1, str2 ) )
        printf("A segunda string esta contida na primeira!\n");

    return 0;
}

However, in the academic world, the challenge is to understand how the strstr() works behind the scenes, follows a second implementation without using the cited function:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>


const char * minha_strstr( const char * palheiro, const char * agulha )
{
    do {
        const char * p = palheiro;
        const char * a = agulha;

        while( (*p == *a) && (*a) )
        {
            a++;
            p++;
        }

        if( *a == 0 )
            return palheiro;

    } while (*palheiro++);

    return NULL;
}


int main( int argc, char * argv[] )
{
    char str1[100];
    char str2[100];

    printf("Forneca a primeira string: ");
    fgets( str1, sizeof(str1), stdin );
    str1[ strcspn(str1, "\n") ] = 0;

    printf("Forneca a segunda string: ");
    fgets( str2, sizeof(str2), stdin );
    str2[ strcspn(str2, "\n") ] = 0;

    if( minha_strstr( str1, str2 ) )
        printf("A segunda string esta contida na primeira!\n");

    return 0;
}

I hope I’ve helped!

Browser other questions tagged

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