How do you write a Strstr function?


Syntax Declaration for strstr function

int strstr(char*, char*);

What Does strstr() Return?

The strstr() function searches the given string in the specified main string and returns the pointer to the first occurrence of the given string.

Does Strstr Modify the String?

All the const char * is telling you is that strstr is not going to modify the string you pass into it. Whether you modify the returned string or not is up to you as it is your string!

How to Use strset

The h header file supports all the string functions in C language.

C String Functions

Function Description
strlwr() Converts string to lowercase
strupr() Converts string to uppercase
strrev() Reverses the given string
strset() Sets all characters in a string to a given character

Is Strstr Case-sensitive?

It’s a cousin to the standard C library string comparison function, strstr(). The word "case" sandwiched inside means that the strings are compared in a case-insensitive way. Otherwise, both functions are identical.

How to Implement Strstr in C++

strStr() Implementation in C++

The following implementation of strStr() method in C++ has a time complexity O(MN) where M and N are the lengths of the input haystack and needle string.

The outer loop should be from 0 to the length of M-N+1. And the inner loop checks if the substring of haystack matches the needle.

Lascia un commento