Javatpoint Logo
Javatpoint Logo

Strstr() function in C

The strstr() function in C is a part of the string.h library and is used to find the first occurrence of a substring within a given string. If the substring is found in the string, it returns a pointer to the first character of the first occurrence of the substring. The function returns a NULL pointer if the substring is not present in the given string.

Syntax:

It has the following syntax:

Here is an explanation of each heading or aspect related to the strstr() function:

Function Name:

The strstr() function stands for "string search". It is a built-in C library function that searches for a substring within a larger string.

Return Type:

The function returns a pointer to the substring ('match')'s first occurrence within the text ('string'). If the substring cannot be found, the method returns NULL.

Parameters:

  • 'string' (const char *): It is the input string in which the substring should be found.
  • 'match' (const char *): It is the substring you want to search for within the string.

Function Behavior:

The strstr() function looks for the first instance of the substring ('match') inside the more extensive string ('string').

It returns a reference to the haystack place where the substring begins or NULL if it cannot be found.

Case Sensitivity:

By default, strstr() is case-sensitive. It differentiates between uppercase and lowercase characters in both the string and the match.

Example Usage:

Output:

Substring is: javatpoint with c and java

Complexity:

Time Complexity: O((n - m + 1) * m)

Auxiliary Space Complexity: O(1)

Explanation:

In the example above, the strstr() function searches for the first occurrence of the substring "java" in the string str. Since "java" is present in the string, the function returns a pointer to the beginning of the matched substring "javatpoint with c and java", which is then printed using the printf() function. The strstr() function returns a pointer to the first occurrence of the matched string in the given string. It returns the substring from the first match to the last character.

Application:

Searching for a specific word within a given text.

Example Code:

Output:

Word' fox' found at index: 16

Complexity:

Time Complexity: O((n - m + 1) * m)

Auxiliary Space Complexity: O(1)

Features of the strstr() function in c

The strstr() function has several useful capabilities for searching and manipulating strings in C. Here are a few of its key characteristics:

  • Substring Search:

The primary purpose of the strstr() function is to search for the first occurrence of a substring within a larger string (match).

  • Pointer Return:

It gives back a pointer to the substring's leading character that was located in the haystack. A NULL reference is returned if the substring is not found.

  • Case Sensitivity:

By default, the strstr() function performs a case-sensitive search. It considers the exact sequence of characters in the needle and haystack.

  • Customizable Search:

While the strstr() function is case-sensitive by default, you can implement case-insensitive searching by converting the haystack and the needle to lowercase or uppercase before performing the search.

  • String Manipulation:

The returned pointer can be used to manipulate the string, such as extracting the substring, modifying it, or performing other operations.

  • Text Processing:

The strstr() function is commonly used in text processing tasks, such as finding keywords or phrases within text, highlighting search terms, and performing basic string-based operations.

  • Efficiency:

Although the worst-case time complexity of the strstr() function is not always optimal (O((n - m + 1) * m)), where n is the length of the haystack & m is the length of the needle, it still performs well for many practical use cases.

  • Library Function:

The strstr() function can be found in the string.h header of the C Standard Library. It indicates that it can be used without extra libraries or dependencies in any C environment.

The strstr() function works with null-terminated strings, like most C string methods. It indicates that it expects strings ending with a null character ('0').

  • Character Set Agnostic:

The strstr() function operates on individual bytes and does not depend on the character encoding used for the strings. It's suitable for various character sets, including ASCII and Unicode.

  • Usage with Other Functions:

The strstr() fucntion can be supplemented with additional string functions like altering substrings, counting occurrences, and separating strings.

  • Portability

As the strstr() function is a component of the C Standard Library, it is accessible on many platforms and compilers, enhancing the portability of your work.

Although strstr() is flexible and effective for many applications, there may be occasions where other string manipulation methods or libraries are more appropriate. This is especially true if you need to perform more in-depth text processing or pattern matching.

Advantages and disadvantages of strstr() function in C:

Like any other tool, the strstr() function in C has its advantages and disadvantages. Let us explore these aspects:

Advantages:

  • Simplicity: The strstr() function is straightforward, especially for basic substring search operations. Its interface is simple and doesn't require complex configurations.
  • Built-in Functionality: It is part of the C Standard Library and is available in virtually every C environment without requiring additional libraries or dependencies.
  • Portability: Since it is a standard library function, code using the strstr() function tends to be more portable across different systems and compilers.
  • Efficiency for Many Use Cases: While its worst-case time complexity isn't optimal (O((n - m + 1) * m)), it performs well for many practical scenarios, making it suitable for basic text processing tasks.
  • Text Processing: It is especially useful for searching for keywords, extracting substrings, and basic string manipulations.
  • Pointer Manipulation: The returned pointer can be used to directly manipulate the string or perform further operations without needing extra memory allocation.

Disadvantages:

  • Limited to First Occurrence: The strstr() function only finds the first occurrence of the substring within the haystack. If you need to find all occurrences, you'll need to implement additional logic.
  • Inefficiency in Some Cases: The worst-case time complexity of the strstr() function can be inefficient for large strings or when searching for long substrings within long haystacks.
  • Case Sensitivity: By default, the strstr() function is case-sensitive. If you require case-insensitive searching, you must convert strings to lowercase or uppercase, which might be time-consuming.
  • Advanced Features Missing: While the strstr() function is adequate for basic jobs, it lacks complex features such as regular expressions and more complicated pattern matching.
  • Not Suitable for Binary Data: It's designed for null-terminated strings, so it might not work well for searching within binary data containing null bytes.
  • Memory Manipulation: While the returned pointer can be used to manipulate the string, improper usage could lead to buffer overflows or memory corruption.
  • Limited Error Reporting: If the substring is not found in the string, the strstr() function primarily returns a pointer to the substring or NULL. It doesn't provide detailed error messages, making debugging more challenging.

Conclusion:

The strstr() function is useful for searching and extracting substrings within C strings. It is widely used in various applications that involve string processing and manipulation. However, it's essential to be mindful of case sensitivity, edge cases, and the potential performance impact when dealing with large strings.


Next TopicC Math Functions



Youtube For Videos Join Our Youtube Channel: Join Now

Feedback


Help Others, Please Share

facebook twitter pinterest

Learn Latest Tutorials


Preparation


Trending Technologies


B.Tech / MCA