Strstr Function In C

It has the following syntax:

Example

char *strstr(const char *string, const char *match);

Here is a breakdown of each header or element associated with the strstr function:

Function Name:

The strstr function, short for "string search", is a predefined function in the C library that scans for a substring within a larger string.

Return Type:

The function provides a pointer to the first instance of the substring ('match') within the text ('string'). In cases where the substring is not located, the function 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 searches for the initial occurrence of the substring ('match') within the larger string ('string').

It provides the location in the haystack where the substring starts or returns NULL if it is not found.

Case Sensitivity:

By default, the strstr function is sensitive to cases. It distinguishes between uppercase and lowercase letters in both the string and the search criteria.

Example Usage:

Example

Example

#include <stdio.h>

#include <string.h>



intmain() {

char str[100] = "this is logic practice with c and java";

char *sub;



sub = strstr(str, "java");



if (sub != NULL) {

printf("Substring is: %s", sub);

} else {

printf("Substring not found.");

}



return 0;

}

Output:

Output

Substring is: logic practice with c and java

Complexity:

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

Auxiliary Space Complexity: O(1)

Explanation:

In the example mentioned, the strstr method is employed to locate the initial occurrence of the subsequence "java" within the string str. Upon finding "java" within the string, the method provides a reference to the start of the corresponding subsequence "logic practice with c and java", which is subsequently displayed utilizing the printf function. The strstr method furnishes a reference to the initial instance of the matched string within the provided string, encompassing the substring from the initial match up to the concluding character.

Application:

Locating a particular term within a provided text.

Example Code:

Example

Example

#include <stdio.h>

#include <string.h>



int main() {

const char *text = "The quick brown fox leaps over the sleepy dog.";

const char *word = "fox";



char *result = strstr(text, word);



if (result != NULL) {

printf("Word '%s' found at index: %ld\n", word, result - text);

} else {

printf("Word '%s' not found.\n", word);

}



return 0;

}

Output:

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 offers various functionalities for searching and manipulating strings in the C programming language. Here are some essential features it provides:

  • String Matching:

The main objective of utilizing the strstr function is to locate the initial instance of a sub-string within a bigger string (match).

  • Pointer Return:

It returns a pointer to the starting character of the located substring within the haystack. If the substring is not found, a NULL pointer is returned.

  • Sensitive to Letter Case:

By default, the strstr function conducts a case-sensitive search, taking into account the precise sequence of characters in both the needle and haystack.

  • Tailored Search Options:

While the strstr function typically distinguishes between cases, you have the option to enable case-insensitive searching by converting both the haystack and the needle to either lowercase or uppercase prior to executing the search operation.

  • String Handling:

The pointer that is received can be utilized for manipulating the string, like extracting a substring, making modifications to it, or executing various operations.

  • Text Manipulation:

The strstr function is frequently employed in text manipulation activities, like locating keywords or phrases in text, emphasizing search terms, and executing fundamental string operations.

  • Effectiveness:

Although the poorest-case time complexity of the strstr method might not always be the most efficient (O((n - m + 1) * m)), with n representing the haystack's length and m representing the needle's length, it remains effective for numerous real-world scenarios.

  • Built-in Function:

The strstr function is located within the string.h header of the C Standard Library. This implies that it is accessible in any C environment without the need for additional libraries or dependencies.

The strstr function operates on strings terminated by a null character, similar to the majority of C string functions. It signifies that the function anticipates strings to conclude with a null character ('0').

  • Independent of Character Set:

The strstr function works at the byte level and is independent of the string's character encoding. It is compatible with different character sets such as ASCII and Unicode.

  • Integration with Additional Functions:

The strstr function can be enhanced with other string functions such as modifying substrings, calculating instances, and splitting strings.

  • Portability

Since the strstr function is part of the C Standard Library, it is available across a variety of platforms and compilers, thereby improving the portability of your codebase.

While strstr is versatile and efficient in numerous scenarios, there are instances where alternative string manipulation techniques or libraries might be better suited. This is particularly relevant when encountering the necessity for extensive text analysis or pattern recognition tasks.

Advantages and disadvantages of strstr function in C:

Similar to other tools, the strstr function in C comes with its own set of benefits and drawbacks. Let's delve into these facets:

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 valuable for locating and retrieving substrings within C strings. It is commonly applied in different scenarios that require handling and modifying strings. Remember to consider case sensitivity, special situations, and the performance implications when working with lengthy strings.

Input Required

This code uses input(). Please provide values below: