> 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_strcmp.md).

# ft\_strcmp

**Objective:**

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

* **Turn-in Directory:** `ex00/`
* **Files to Turn In:** `ft_strcmp.c`
* **Allowed Functions:** None

**Prototype:**

```c
int ft_strcmp(char *s1, char *s2);
```

***

#### **Detailed Explanation:**

* The function compares two strings `s1` and `s2` character by character.
* It returns:
  * `0` if the strings are identical.
  * A **positive value** if `s1` is lexicographically greater than `s2`.
  * A **negative value** if `s1` is lexicographically smaller than `s2`.

***

#### **Code Implementation:**

```c
int ft_strcmp(char *s1, char *s2)
{
	while (*s1 == *s2 && *s1)  // Continue as long as characters are equal and not at the end of the string
	{
		s1++;  // Move to the next character in s1
		s2++;  // Move to the next character in s2
	}
	return (*s1 - *s2);  // Return the ASCII difference of the first differing characters
}
```

***

#### **Example Usage:**

```c
#include <stdio.h>

int ft_strcmp(char *s1, char *s2);

int main(void)
{
    printf("Comparison 1: %d\n", ft_strcmp("hello", "hello"));  // Output: 0
    printf("Comparison 2: %d\n", ft_strcmp("hello", "hell"));   // Positive value
    printf("Comparison 3: %d\n", ft_strcmp("hell", "hello"));   // Negative value
    printf("Comparison 4: %d\n", ft_strcmp("abc", "abd"));      // Negative value
    printf("Comparison 5: %d\n", ft_strcmp("xyz", "xy"));       // Positive value

    return 0;
}
```

***

#### **Edge Cases to Consider:**

* **Identical Strings:** `"hello", "hello"` → Returns `0`
* **One String is a Prefix:** `"hello", "hell"` → Returns a positive value
* **Different Characters in Between:** `"abc", "abd"` → Returns a negative value
* **Empty String Comparisons:** `"" , ""` → Returns `0`
* **Comparing With an Empty String:** `"hello", ""` → Returns a positive value

#### **Limitations of `ft_strcmp`:**

* **Case Sensitivity:** It performs case-sensitive comparisons (`"Hello"` is different from `"hello"`).
* **No Bounds Checking:** If given non-null-terminated strings, it may lead to undefined behavior.
* **Lexicographical Order:** The function doesn’t return `1` or `-1`, but rather the ASCII difference, which might not always be intuitive.


---

# 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_strcmp.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.
