Doubt in string, and comparison

1

I'm 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, that is, if the   second string is a segment of the first. You may consider, in your   program, that the second string is always smaller than the first, and that the   strings have a maximum of 100 characters. Here is an example of   entrance and exit. What is underlined was provided by the user.

     
  • String 1: program
  •   
  • String 2: gram
  •   

The second string is contained in the first!

The code I made, always says "The second string is contained in the first string". Here is 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;
}
    
asked by anonymous 05.05.2016 / 16:04

2 answers

2

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

string2[i]=string1[i]

You are overwriting the i-nth character of string2 . And yes, within%% of an assignment is true, then what is within if is executed.

To compare, use if .

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 the same.

Instead, do this:

  • Run all the characters in the larger string until you find one that is the same as the first character in the smaller string.
  • From this point on, if the next N-1 characters of the largest string (where N is the smallest string length) are equal to the next N-1 characters of the smallest, the smallest is contained in the largest.
  • If the above step is not conclusive, continue looking for another character in the largest string that is equal to the first character of the smaller string and repeat the second step.
  • If the (M - N) th (where M is the largest string size, and N the smaller size) is the largest string character and has not found the smallest string inside it, the smallest string is not contained in string greater.

Good luck!

    
05.05.2016 / 16:26
1

The strstr() function of the default library checks if a 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 academic circles, the challenge is to understand how strstr() works underneath the cloths, it follows a second implementation without using the function mentioned:

#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 have helped!

    
07.05.2016 / 00:11