> 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/c02/ft_strcapitalize.md).

# ft\_strcapitalize

####

**Objective:**

The function `ft_strcapitalize` modifies a given string by capitalizing the first letter of each word while converting all other letters in the word to lowercase. A "word" is defined as a sequence of alphanumeric characters.

***

#### **Turn-in Requirements:**

* **Directory:** `ex09/`
* **File:** `ft_strcapitalize.c`
* **Allowed Functions:** None

***

#### **Prototype:**

```c
char *ft_strcapitalize(char *str);
```

***

#### **Implementation:**

```c
char	*ft_strcapitalize(char *str)
{
	int		new_word; // Flag to check if the current character is the start of a new word
	int		i;
	char	c;

	i = 0;
	new_word = 1; // The first character is always the start of a new word
	while (str[i] != '\0') // Iterate through the entire string
	{
		c = str[i];
		
		// If it's the start of a new word and the character is lowercase, convert it to uppercase
		if (new_word == 1 && c >= 'a' && c <= 'z')
			str[i] = str[i] - 32; // Convert lowercase to uppercase
		
		// If it's not a new word and the character is uppercase, convert it to lowercase
		else if (new_word == 0 && c >= 'A' && c <= 'Z')
			str[i] = str[i] + 32; // Convert uppercase to lowercase
		
		// If the character is non-alphanumeric, set new_word flag to 1 (start a new word)
		if (c < '0' || (c > '9' && c < 'A') || (c > 'Z' && c < 'a') || c > 'z')
			new_word = 1;
		else
			new_word = 0; // Otherwise, continue within the current word
		
		i++; // Move to the next character
	}
	return (str); // Return the modified string
}
```

***

#### **Explanation of the Logic:**

1. **Tracking Word Boundaries:**
   * We use the `new_word` flag to determine whether the current character is the start of a new word.
   * If a character follows a non-alphanumeric character, it's considered the first letter of a new word.
2. **Character Conversion:**
   * If `new_word == 1` and the character is lowercase (`'a' - 'z'`), it is converted to uppercase.
   * If `new_word == 0` and the character is uppercase (`'A' - 'Z'`), it is converted to lowercase.
3. **Identifying Non-Alphanumeric Characters:**
   * If the character is **not a letter or number**, it is considered a word separator, and `new_word` is set to `1`.
4. **Moving to the Next Character:**
   * The loop continues until it reaches the end of the string (`'\0'`).

***

#### **Example Usage:**

```c
#include <stdio.h>

char *ft_strcapitalize(char *str);

int main()
{
    char str1[] = "salut, comment tu vas ? 42mots quarante-deux; cinquante+et+un";
    char str2[] = "HELLO WORLD";
    char str3[] = "already Capitalized words";
    char str4[] = "";

    printf("%s\n", ft_strcapitalize(str1));
    printf("%s\n", ft_strcapitalize(str2));
    printf("%s\n", ft_strcapitalize(str3));
    printf("%s\n", ft_strcapitalize(str4));

    return 0;
}
```

#### **Expected Output:**

```php
Salut, Comment Tu Vas ? 42mots Quarante-Deux; Cinquante+Et+Un
Hello World
Already Capitalized Words
(empty string remains unchanged)
```

***

#### **Edge Cases Covered:**

* Handles words separated by spaces, punctuation, and special characters.
* Works correctly for empty strings.
* Ensures all non-initial uppercase letters are converted to lowercase.

***

#### **Complexity Analysis:**

* The function iterates through the string **once**, making it **O(n)** in time complexity, where **n** is the length of the string.
* The function modifies the input string **in place**, meaning it has **O(1)** space complexity.


---

# 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/c02/ft_strcapitalize.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.
