> For the complete documentation index, see [llms.txt](https://42-guide.gitbook.io/42-guide/llms.txt). Markdown versions of documentation pages are available by appending `.md` to page URLs; this page is available as [Markdown](https://42-guide.gitbook.io/42-guide/piscine-life/c03/ft_strstr.md).

# ft\_strstr

**Objective:**

Recreate the behavior of the `strstr` function (refer to `man strstr`).

**Turn-in Directory: `ex04/`**

**Files to Turn In: `ft_strstr.c`**

**Allowed Functions: None**

**Prototype:**

```c
char *ft_strstr(char *str, char *to_find);
```

***

#### **Detailed Explanation:**

**1. Functionality:**

The `ft_strstr` function searches for the first occurrence of the substring `to_find` in the string `str`. If the substring is found, it returns a pointer to the beginning of the located substring. If the substring is not found, it returns a null pointer (`0`).

**2. Input Parameters:**

* `char *str`: A pointer to the string to be searched.
* `char *to_find`: A pointer to the substring to search for.

**3. Output:**

* Returns a pointer to the first occurrence of `to_find` in `str`.
* Returns `0` if `to_find` is not found in `str`.

***

#### **Steps to Implement:**

1. **Check if `to_find` is Empty:**
   * If `to_find` is an empty string (`to_find[0] == '\0'`), return `str` immediately.
2. **Search for `to_find` in `str`:**
   * Use a loop to iterate through each character of `str`.
   * For each character in `str`, use a nested loop to compare it with `to_find`.
3. **Compare Characters:**
   * If a match is found, continue comparing subsequent characters.
   * If the entire `to_find` substring is matched, return a pointer to the starting position in `str`.
4. **Handle No Match:**
   * If no match is found after iterating through `str`, return `0`.

***

#### **Code Explanation:**

```c
char	*ft_strstr(char *str, char *to_find)
{
	int	i;
	int	j;

	i = 0;

	// If to_find is an empty string, return str immediately
	if (to_find[0] == '\0')
		return (str);

	// Iterate through each character of str
	while (str[i] != '\0')
	{
		j = 0;

		// Compare str[i + j] with to_find[j]
		while (str[i + j] == to_find[j] && to_find[j] != '\0')
		{
			j++;
		}

		// If to_find is fully matched, return the pointer to the starting position in str
		if (to_find[j] == '\0')
			return (&str[i]);

		// Move to the next character in str
		i++;
	}

	// If no match is found, return 0
	return (0);
}
```

***

#### **Step-by-Step Breakdown:**

1. **Check if `to_find` is Empty:**
   * If `to_find[0] == '\0'`, the function immediately returns `str` because an empty string is always considered a match.
2. **Iterate Through `str`:**
   * The outer `while (str[i] != '\0')` loop iterates through each character of `str`.
3. **Compare Characters:**
   * The inner `while (str[i + j] == to_find[j] && to_find[j] != '\0')` loop compares characters of `str` and `to_find`.
   * If a match is found, `j` is incremented to compare the next character.
4. **Check for Full Match:**
   * If `to_find[j] == '\0'`, it means the entire `to_find` substring has been matched, so the function returns a pointer to the starting position in `str` (`&str[i]`).
5. **Move to the Next Character:**
   * If no match is found at the current position, `i` is incremented to check the next character in `str`.
6. **Handle No Match:**
   * If the outer loop completes without finding a match, the function returns `0`.

***

#### **Example Usage:**

```c
#include <stdio.h>

char	*ft_strstr(char *str, char *to_find);

int	main(void)
{
	char str[] = "Hello, 42 School!";
	char to_find[] = "42";

	char *result = ft_strstr(str, to_find);

	if (result)
		printf("Substring found: %s\n", result);
	else
		printf("Substring not found.\n");

	return (0);
}
```

**Expected Output:**

```
Substring found: 42 School!
```

***

#### **Edge Cases to Test:**

1. **Empty `to_find` String:**
   * Input: `str = "Hello, 42!"`, `to_find = ""`
   * Expected Output: `str` (pointer to the beginning of `str`)
2. **`to_find` at the Beginning of `str`:**
   * Input: `str = "42 School!"`, `to_find = "42"`
   * Expected Output: `"42 School!"`
3. **`to_find` at the End of `str`:**
   * Input: `str = "Hello, 42!"`, `to_find = "42!"`
   * Expected Output: `"42!"`
4. **`to_find` Not in `str`:**
   * Input: `str = "Hello, World!"`, `to_find = "42"`
   * Expected Output: `0` (null pointer)
5. **Partial Match:**
   * Input: `str = "Hello, 42!"`, `to_find = "42 School"`
   * Expected Output: `0` (null pointer, because the full substring is not found)
6. **Special Characters in `to_find`:**
   * Input: `str = "Hello\nWorld\t42!"`, `to_find = "\nWorld"`
   * Expected Output: `"\nWorld\t42!"`

***

#### **Limitations of `ft_strstr`:**

* The function does not handle `NULL` pointers. Passing `NULL` will result in undefined behavior.
* The function assumes valid strings (null-terminated). Non-null-terminated strings may cause unexpected behavior.


---

# Agent Instructions
This documentation is published with GitBook. GitBook is the documentation platform designed so that both humans and AI agents can read, navigate, and reason over technical content effectively. Learn more at gitbook.com.

## Querying This Documentation
If you need additional information that is not directly available in this page, you can query the documentation dynamically by asking a question.

Perform an HTTP GET request on the current page URL with the `ask` query parameter:

```
GET https://42-guide.gitbook.io/42-guide/piscine-life/c03/ft_strstr.md?ask=<question>
```

The question should be specific, self-contained, and written in natural language.
The response will contain a direct answer to the question and relevant excerpts and sources from the documentation.

Use this mechanism when the answer is not explicitly present in the current page, you need clarification or additional context, or you want to retrieve related documentation sections.
