How to check if one string is contained in another in C?

Asked

Viewed 9,247 times

1

I have two variables:

char s1[20];
char s2[20];

I’ll go get the input user of the two variables, and after this, I need to check whether s1 is contained in s2, even a part of it. For this I will use a function called esta_contido() who will make this check.

Example:

    s1 = "algoritmo"; 
    s2 = "ritmo";
    esta_contido(); retorna 4 --> pois ele retorna a primeira posição do caractere em que está contido.

My code:

char s1[20];
char s2[20];

void esta_contido() {

    printf("Digite uma string : ");
    scanf("%s",&s1);

    printf("Digite outra string : ");
    scanf("%s",&s2);


    if (strstr(s1, s2) != NULL) { 

    for(int i = 0; i < 20; i++) {
        if(s2[i] == s1[i]) {
            i = 20;
            printf("s2 : %s", &s2);
        }
    }

    }


}


int main() {
    setlocale(LC_ALL,"portuguese");

    esta_contido();


}

On the part of if with for I’m checking to see if s1 and s2 has some equal part, if they have it goes pro go, to verify which words are equal, after that he gives a output pro user the words that are equal. In this I have already achieved some progress, but I need to give output pro user not the letters, but the initial position in which is contained the string.

  • 2

    if (strstr(S1, s2) != NULL) { // contains }

  • Thanks for the code, what is missing is how I can know in what initial position is the letter in which it is contained. Using a for to traverse the entire string would work ?

  • To strstr provides this information in a slightly less direct way. I think with a little bit of pointer arithmetic one rescues exactly this information you need

  • I’m new to C, and pointers are still a bit complex for me, could you explain to me a little more how I can do this check ? I looked at strchr() function but I’m not able to reproduce what I want, I’m now trying to convert char to int to be able to show the Dice of its position, but unsuccessfully too.

  • Do you just want to get the position or do you want to make an algorithm that calculates the position? You don’t have to know anything about pointer in this case.

  • I just want to get the first position where the string is contained. Just as in the above example in which I gave, if the user enters for example with the string "algorithm" and then in another string "rhythm", it will return the first position in which it is contained, in this case position 4.

Show 1 more comment

2 answers

3


As there is already a function ready that takes a part of the string, rather make a substitute from where found the second substring at first string.

There are some other errors in this code and it doesn’t even compile.

#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);
}

Behold working in the ideone. And in the repl it.. Also put on the Github for future reference.

Po of being you want to check if strstr(s1, s2) is void to inform that there is no string inside the other.

  • Thank you so much @bigown as always you helped quickly in what I needed, when I saw your answer, I already knew you would help me and really helped me. This is what I needed, all that was left for the code to be ready the way I wanted it. Just forgive me ignorance, which would be %Ld ?

  • IS long int, depending on the compiler it will get a return of this type.

  • Ah all right, thank you very much for the explanation.

-1

#include<stdio.h>
#include<string.h>
void main(){
    char S1[100], S2[100];
    int i,j,c=0;
    printf("digite a string S1\n");
    gets(S1);
    printf("digite a string S2\n");
    gets(S2);
    if(strlen(S1)<=strlen(S2)){
        printf("S2 nao he substring de S1\n");
    }else{
        for(i=0;i<strlen(S1);i++){
            if(S2[0]==S1[i]){
                for(j=0;j<strlen(S2)-1;j++){
                    if(S2[j]==S1[i++])
                        c++;
                }
            }
            if(c==strlen(S2)){
                break;
        }
    }
    if(c==strlen(S2))
        printf("S2 he string de S1\n");
    else
        printf("S2 nao he string de s1\n");
    }
}

Browser other questions tagged

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