1

I'm trying to write a string routine in C, and I keep hitting on the same issue.

In C, I have this string:

MAMAAMAAALJ

If I have this string:

AAA

How can I determine that AAA is inside of MAMAAMAAAJ?

3 Answers 3

5

Many C runtime libraries contain the function strstr (const char *s1, const char *s2).

If s2 is within s1, it returns a pointer within s1 to the beginning of the substring, otherwise returns NULL.

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

3 Comments

All conforming hosted implementations of C provide strstr.
How many C implementations are conforming? In my experience, a few have some other name for this function and a few don't offer anything too close to it.
I think pretty much every general-purpose C compiler supports strstr. Many environments also include the POSIX standard index function in <strings.h>, but ANSI C is certainly more widespread.
1
strstr("MAMAAMAAAJ", "AAA");

returns the pointer to the occurrence of the search string, or NULL if not found

Comments

1

Boyer–Moore string search algorithm


C realization

2 Comments

I don't know whether to upvote or downvote, so I will not vote.
BM for very short string, im in same situasion as stefan.

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.