0

I have an array that looks like this:

char test[100]

then I want to compare whether or not this array has this specific sentence

if (test == "yup this is the sentence") {
     // do stuff
}

Is this correct? Is there a better approach to this? Thank you.

2 Answers 2

3

You cannot do that in C. What you're doing here is checking identity equality (id est if your two pointers point the same memory zone).

What you can do is use libc strstr that does what you want :

#include <string.h>

if (strstr(test, "yup this is the sentence") != NULL)
{
    // do stuff if test contains the sentence
}

Type man 3 strstr in a terminal to get more info about the function, and all its behaviours.

If you want to understand the behaviour of the function, here it is recoded in pure C, with one loop only :

char        *strstr(const char *s1, const char *s2)
{
    int     begin;
    int     current;

    begin = 0;
    current = 0;

    if (!*s2)
        return ((char *)s1);

    while (s1[begin])
    {
        if (s1[begin + current] == s2[current])
            current++;
        else
        {
            current = 0;
            begin++;
        }
        if (!s2[current])
            return ((char *)s1 + begin);
    }
    return (0);
}

It is part from a school project. The full project contains all the C library basic functions.

You can check out some other string manipulation functions here : https://github.com/kube/libft/tree/master/src/strings

Sign up to request clarification or add additional context in comments.

3 Comments

What exactly you mean "You cannot do that in C"? Please explain.
Ah you are right, my assumption is base on "high-level language". I forgot, it is C. Thanks.
I added an example of the recoded function and a link.
2

You could use strstr:

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

int main(void) {
    char test[256] = "This is a looooooonnnnggggg string which contains ('yup this is the sentence') my needed string inside";

    if (strstr(test, "yup this is the sentence") != NULL){
        printf("True\n");
    }else{
        printf("False\n");
    }
    return 0;
}

or you could use some pointer arithmetic:

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

void checkString(char *string1, char *string2);

int main(void){
    char test[256] = "This is a looooooonnnnggggg string which contains ('yup this is the sentence') my needed string inside";
    char string2[] = "yup this is the sentence";

    checkString(test, string2);
    return 0;
}

void checkString(char *string1, char *string2){
    char *s1, *s2, *s3;

    size_t lenstring1 = strlen(string1);
    size_t lenstring2 = strlen(string2);

    if (lenstring2 < 1){
        printf("There is no substring found");
        exit(1);
    }

    size_t i=0,j=0;
    int found=0;

    s1 = string1;
    s2 = string2;


    for(i = 0; i < lenstring1; i++){
        if(*s1 == *s2){
            s3 = s1;
            for(j = 0;j < lenstring2;j++){
                if(*s3 == *s2){
                  s3++;s2++;
                }else{
                    break;
                }
            }

            s2 = string2;
            if(j == strlen(string2)){
                found = 1;
                printf("%s\nwas found at index : %zu\n",string2,i+1);
              }
          }
        s1++;
    }

    if(found == 0){
        printf("No match Found");
    }
}

Output:

yup this is the sentence
was found at index : 53

1 Comment

Thank you michi! This works as well when I apply it on my code

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.