0

I have two char arrays char search_array[8] and input_array[30] and I'm getting the characters by using getchar, but I want to find if the search array exists in input array without using string library.

Example:

search array: hello

input array hello, how are you

2
  • 4
    just write you own own function, what blocks you ? Commented Apr 5, 2019 at 9:23
  • @Martina: you can accept one of the answers by clicking on the grey checkmark below its score. Commented Apr 13, 2019 at 11:45

2 Answers 2

2

You need some string comparison loop, like that of which you can find in strcmp or strcspn. This is assuming you use a terminal value like C strings; if you use some length field you'll need to make adjustments to the loops to account for those clearly. Here are some examples:

int strcmp(char const *x, char const *y) {
    while (*x && *x == *y) {
        x++;
        y++;
    }
    return ((unsigned)*x > (unsigned)*y) - ((unsigned)*x < (unsigned)*y);
}

size_t strcspn(char const *x, char const *y) {
    char const *x_ = x;
    while (*x_) {
        for (char const *y_ = y; *y_; y_++) {
            if (*x_ == *y_)
                return x_ - x;
        }
        x_++;
    }
    return x_ - x;
}
Sign up to request clarification or add additional context in comments.

5 Comments

This does not answer the OP's question. There is a small bug in your strcmp implementation: the characters should be converted to unsigned char for comparison in the return statement. In strcspn, you should duplicate the return statement instead of using goto.
@chqrlie Hmmm. Interesting OP choose the equivalent (unsigned)*x > (unsigned)*y). It is the same result as (unsigned char)*x > (unsigned char)*y).
@chux: interesting indeed! It seems to evaluate to the same result, but is not an elegant alternative.
@chqrlie Maybe not as elegant (as I first thought too) yet does have a potential advantage as many compilers emit better code using int/unsigned than char types. Of course this is all micro-optimizations.
@chux: indeed micro-optimisations... But then return (unsigned char)*x - (unsigned char)*y; is even better on architectures where UCHAR_MAX < INT_MAX.
1

The obvious solution for your goal is to use the standard library's strstr function.

Since you are not allowed to use the string library, you should write your own version of strstr and use that.

Here is a simple yet conforming implementation:

char *my_strstr(const char *s1, const char *s2) {
    for (;;) {
        for (size_t i = 0;; i++) {
            if (s2[i] == '\0')
                return (char *)s1;
            if (s1[i] != s2[i])
                break;
        }
        if (*s1++ == '\0')
            return NULL;
    }
}

You then use this function this way:

    if (my_strstr(input_array, search_array)) {
        printf("string was found\n");
    } else {
        printf("string was not found\n");
    }

2 Comments

The problem is, that I don't wanna use the string library, I'm not sure how to iterate over the arrays and check if the searched word is found in the input.
If you are allowed to write a function, you can write your own version of strstr and use it with search_array and input_array. If you are not allowed to write functions, you could duplicate the above code, initializing s1 to point to input_array and s2 to point to search_array...

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.