libft
Loading...
Searching...
No Matches
Files | Functions
String Manipulation Utilities

Functions for handling and manipulating C strings. More...

Files

file  ft_split.c
 Splits a string into substrings using a single delimiter character.
 
file  ft_split_charset.c
 Splits a string into substrings based on a set of delimiters.
 
file  ft_string.c
 Returns the number of characters in a string.
 
file  ft_string.h
 String manipulation utility functions.
 
file  ft_strtrim.c
 Trims characters from the beginning and end of a string.
 
file  skip_whitespace.c
 Utility functions to skip leading whitespace in C strings.
 

Functions

char * ft_itoa (int n)
 Converts an integer to a string.
 
char ** ft_split (const char *s, char c)
 Splits a string into substrings based on a delimiter character.
 
char ** ft_split_charset (char *s, char *charset)
 Splits a string into substrings based on a character set.
 
char * ft_strcat (char *dest, const char *src)
 Appends the src string to the end of the dest string.
 
char * ft_strchr (const char *str, int c)
 Finds the first occurrence of a character in a string.
 
int ft_strcmp (const char *s1, const char *s2)
 Compares two strings lexicographically.
 
char * ft_strcpy (char *dst, const char *src)
 Compares two strings lexicographically.
 
char * ft_strdup (const char *src)
 Allocates and duplicates a null-terminated string.
 
void ft_striteri (char *s, void(*f)(unsigned int, char *))
 Applies a function to each character of a string, passing its index.
 
char * ft_strjoin (const char *s1, const char *s2)
 Concatenates two strings into a newly allocated string.
 
char * ft_strjoin_and_free (char *s1, char *s2, int free_s1, int free_s2)
 
size_t ft_strlcat (char *dest, const char *src, size_t size)
 Concatenates src to the end of dest while ensuring null-termination.
 
size_t ft_strlcpy (char *dest, const char *src, size_t size)
 Copies a string from src to dest, up to size - 1 characters.
 
size_t ft_strlen (const char *str)
 
char * ft_strmapi (char const *s, char(*f)(unsigned int, char))
 Applies a function to each character of a string to create a new string.
 
int ft_strncmp (const char *s1, const char *s2, size_t n)
 Compares two strings up to n characters.
 
char * ft_strndup (const char *src, size_t n)
 Creates a copy of a string, up to n characters.
 
char * ft_strnstr (const char *big, const char *little, size_t len)
 Searches for a substring in a string within a max length.
 
char * ft_strrchr (const char *str, int c)
 Returns a pointer to the last occurrence of a character in a string.
 
char * ft_strtrim (const char *s1, const char *set)
 Trims characters from both ends of a string.
 
char * ft_substr (const char *s, unsigned int start, size_t len)
 Extracts a substring from a string.
 
int ft_tolower (int c)
 Converts a character to lowercase.
 
int ft_toupper (int c)
 Converts a character to uppercase.
 
void skip_whitespace_index (const char *input, int *i)
 Skips leading whitespace by incrementing a pointer index.
 
char * skip_whitespace_ptr (const char *s)
 Returns a pointer to the first non-whitespace character.
 

Detailed Description

Functions for handling and manipulating C strings.

This group includes common string operations such as copying, concatenation, trimming, splitting, searching, and case conversion.

Useful for tasks like:

Function Documentation

◆ ft_itoa()

char * ft_itoa ( int  n)

#include <srcs/convert/ft_number_conversion.c>

Converts an integer to a string.

Allocates and returns a new string representing the integer n. Handles negative numbers and returns NULL if allocation fails.

Parameters
nThe integer to convert.
Returns
A null-terminated string representing n, or NULL on failure.

◆ ft_split()

char ** ft_split ( const char *  s,
char  c 
)

#include <include/ft_string.h>

Splits a string into substrings based on a delimiter character.

Splits the input string s into substrings separated by the delimiter c. The resulting substrings are stored in a newly allocated NULL-terminated array.

Parameters
sThe string to split.
cThe delimiter character.
Returns
A newly allocated array of substrings, or NULL on failure.
Note
The input string is not modified. Each substring and the array itself must be freed manually using ft_free_array.
See also
ft_w_count
ft_fill_array
ft_substr
ft_free_array

◆ ft_split_charset()

char ** ft_split_charset ( char *  s,
char *  charset 
)

#include <include/ft_string.h>

Splits a string into substrings based on a character set.

This function splits the input string s into substrings using the set of delimiter characters defined in charset. Each substring is allocated dynamically and stored in a NULL-terminated array.

Parameters
sThe string to split.
charsetThe set of delimiter characters.
Returns
A newly allocated array of substrings, or NULL on error.
See also
ft_w_count_charset
ft_fill_array_charset
ft_free_array

◆ ft_strcat()

char * ft_strcat ( char *  dest,
const char *  src 
)

#include <include/ft_string.h>

Appends the src string to the end of the dest string.

This function assumes that dest points to a buffer large enough to hold the resulting string, including the null terminator.

Parameters
destThe destination string to which src will be appended.
srcThe source string to append.
Returns
A pointer to the destination string (dest).
Note
The behavior is undefined if the destination buffer is not large enough to contain the result.

◆ ft_strchr()

char * ft_strchr ( const char *  str,
int  c 
)

#include <include/ft_string.h>

Finds the first occurrence of a character in a string.

Searches for the first occurrence of the character c in the string str. The terminating null byte (\0) is considered part of the string.

Parameters
strThe null-terminated string to search in.
cThe character to search for, interpreted as an unsigned char.
Returns
A pointer to the first occurrence of c in str, or NULL if c is not found.

◆ ft_strcmp()

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

#include <include/ft_string.h>

Compares two strings lexicographically.

This function compares the characters of s1 and s2 one by one. The comparison stops at the first differing character or when a null byte is reached.

Parameters
s1Pointer to the first null-terminated string.
s2Pointer to the second null-terminated string.
Returns
An integer less than, equal to, or greater than 0 if s1 is found to be less than, equal to, or greater than s2, respectively.
Note
The comparison is done using unsigned char values.
See also
ft_strncmp
ft_strcmp

◆ ft_strcpy()

char * ft_strcpy ( char *  dst,
const char *  src 
)

#include <include/ft_string.h>

Compares two strings lexicographically.

This function compares the characters of s1 and s2 one by one. The comparison stops at the first differing character or when a null byte is reached.

Parameters
s1Pointer to the first null-terminated string.
s2Pointer to the second null-terminated string.
Returns
An integer less than, equal to, or greater than 0 if s1 is found to be less than, equal to, or greater than s2, respectively.

◆ ft_strdup()

char * ft_strdup ( const char *  src)

#include <include/ft_string.h>

Allocates and duplicates a null-terminated string.

Allocates memory for a copy of the string src, copies its contents (including the null terminator), and returns a pointer to the new string.

Parameters
srcThe source string to duplicate.
Returns
A pointer to the newly allocated string, or NULL on allocation failure.
See also
ft_strlcpy
ft_strlen

◆ ft_striteri()

void ft_striteri ( char *  s,
void(*)(unsigned int, char *)  f 
)

#include <include/ft_string.h>

Applies a function to each character of a string, passing its index.

Iterates over the string s and applies the function f to each character. The function receives the index of the character and a pointer to it, allowing in-place modification.

Parameters
sThe string to iterate over.
fThe function to apply to each character.
Note
If either s or f is NULL, the function does nothing.

◆ ft_strjoin()

char * ft_strjoin ( const char *  s1,
const char *  s2 
)

#include <include/ft_string.h>

Concatenates two strings into a newly allocated string.

Allocates a new string containing the contents of s1 followed by s2. If either string is NULL, it is treated as an empty string.

Parameters
s1The first string.
s2The second string.
Returns
A new string containing the concatenation of s1 and s2, or NULL if memory allocation fails.
Note
The caller is responsible for freeing the returned string.
See also
ft_strlen
ft_strlcpy

◆ ft_strjoin_and_free()

char * ft_strjoin_and_free ( char *  s1,
char *  s2,
int  free_s1,
int  free_s2 
)

#include <include/ft_string.h>

◆ ft_strlcat()

size_t ft_strlcat ( char *  dest,
const char *  src,
size_t  size 
)

#include <include/ft_string.h>

Concatenates src to the end of dest while ensuring null-termination.

This function appends the src string to the end of the dest string. It ensures that the result fits within the buffer size given by size, including the null-terminator. If size is less than or equal to the length of dest, the function returns size + strlen(src) without appending.

Parameters
destThe destination buffer.
srcThe source string to append.
sizeThe total size of the destination buffer.
Returns
The total length of the string it tried to create: strlen(dest) + strlen(src). This value can be used to detect truncation.
See also
ft_strlen
ft_strlcpy

◆ ft_strlcpy()

size_t ft_strlcpy ( char *  dest,
const char *  src,
size_t  size 
)

#include <include/ft_string.h>

Copies a string from src to dest, up to size - 1 characters.

Copies up to size - 1 characters from the null-terminated string src to dest, null-terminating the result. If size is 0, nothing is written to dest, but the length of src is still returned.

Parameters
destDestination buffer.
srcSource null-terminated string to copy.
sizeTotal size of destination buffer.
Returns
The length of src. This allows detection of truncation: if the return value is >= size, truncation occurred.

◆ ft_strlen()

size_t ft_strlen ( const char *  str)

#include <include/ft_string.h>

◆ ft_strmapi()

char * ft_strmapi ( char const *  s,
char(*)(unsigned int, char)  f 
)

#include <include/ft_string.h>

Applies a function to each character of a string to create a new string.

Allocates a new string where each character is the result of applying the given function f to the corresponding character of the input string s. The function f receives the index of each character as its first argument.

Parameters
sThe original null-terminated string to map.
fThe function to apply to each character, receiving the index and the character.
Returns
A new string with transformed characters, or NULL if allocation fails or if input is NULL.
Note
The returned string must be freed by the caller.

◆ ft_strncmp()

int ft_strncmp ( const char *  s1,
const char *  s2,
size_t  n 
)

#include <include/ft_string.h>

Compares two strings up to n characters.

This function compares the first n characters of the strings s1 and s2. The comparison is done using unsigned characters.

Parameters
s1First string to compare.
s2Second string to compare.
nMaximum number of characters to compare.
Returns
An integer less than, equal to, or greater than 0 if s1 is found, respectively, to be less than, to match, or be greater than s2.
Note
The comparison stops when a difference is found, a null byte is encountered, or n characters have been compared.

◆ ft_strndup()

char * ft_strndup ( const char *  src,
size_t  n 
)

#include <include/ft_string.h>

Creates a copy of a string, up to n characters.

This function allocates memory and copies at most n characters from the string src to a new string. The new string is always null-terminated.

Parameters
srcThe source string to duplicate.
nThe maximum number of characters to copy.
Returns
A pointer to the newly allocated string, or NULL if memory allocation fails.
See also
ft_strlcpy
ft_strlen

◆ ft_strnstr()

char * ft_strnstr ( const char *  big,
const char *  little,
size_t  len 
)

#include <include/ft_string.h>

Searches for a substring in a string within a max length.

This function searches for the first occurrence of the null-terminated string little in the string big, where not more than len characters are searched. Characters after a \0 character are not searched.

If little is an empty string, big is returned. If little occurs nowhere in big within the first len characters, NULL is returned.

Parameters
bigThe main string to be searched.
littleThe substring to search for.
lenThe maximum number of characters to search in big.
Returns
A pointer to the first occurrence of little in big within len characters, or NULL if not found.

◆ ft_strrchr()

char * ft_strrchr ( const char *  str,
int  c 
)

#include <include/ft_string.h>

Returns a pointer to the last occurrence of a character in a string.

Scans the string str for the last occurrence of the character c (interpreted as an unsigned char). If found, a pointer to the character is returned. If not, NULL is returned.

If c is '\0', a pointer to the null terminator at the end of the string is returned.

Parameters
strThe null-terminated string to be searched.
cThe character to search for (casted to unsigned char).
Returns
A pointer to the last occurrence of c in str, or NULL if not found.

◆ ft_strtrim()

char * ft_strtrim ( const char *  s1,
const char *  set 
)

#include <include/ft_string.h>

Trims characters from both ends of a string.

Allocates and returns a new string derived from s1 with all characters found in set removed from both the beginning and the end. The returned string is NULL-terminated and dynamically allocated.

Parameters
s1The original null-terminated string to trim.
setThe set of characters to remove.
Returns
A newly allocated trimmed string, or NULL on allocation failure.
Note
If the trimmed result is empty, the function returns a string containing only '\0'. The caller is responsible for freeing the result.
See also
ft_trim_start
ft_trim_end
ft_strlcpy

◆ ft_substr()

char * ft_substr ( const char *  s,
unsigned int  start,
size_t  len 
)

#include <include/ft_string.h>

Extracts a substring from a string.

Allocates and returns a new string, which is a substring of s starting from index start and up to len characters long. If start is greater than or equal to the length of s, an empty string is returned. The length of the returned substring is either len or the remaining length from start, whichever is smaller.

Parameters
sThe source string.
startThe starting index in the source string.
lenThe maximum number of characters to copy.
Returns
A newly allocated string containing the substring, or NULL if memory allocation fails. If start is out of bounds, an empty string is returned.
See also
ft_strlen
ft_strlcpy

◆ ft_tolower()

int ft_tolower ( int  c)

#include <include/ft_string.h>

Converts a character to lowercase.

If the character c is an uppercase letter (between 'A' and 'Z'), it is converted to its lowercase equivalent by adding the difference between the ASCII values of 'a' and 'A'. If c is not an uppercase letter, the function returns c unchanged.

Parameters
cThe character to convert.
Returns
The lowercase version of c if it is an uppercase letter; otherwise, returns c unchanged.
See also
ft_toupper

◆ ft_toupper()

int ft_toupper ( int  c)

#include <include/ft_string.h>

Converts a character to uppercase.

If the character c is a lowercase letter (between 'a' and 'z'), it is converted to its uppercase equivalent by subtracting the difference between the ASCII values of 'a' and 'A'. If c is not a lowercase letter, the function returns c unchanged.

Parameters
cThe character to convert.
Returns
The uppercase version of c if it is a lowercase letter; otherwise, returns c unchanged.
See also
ft_tolower

◆ skip_whitespace_index()

void skip_whitespace_index ( const char *  input,
int *  i 
)

#include <include/ft_string.h>

Skips leading whitespace by incrementing a pointer index.

This function iterates through the input string from the position indicated by the index *i, incrementing the index until a non-whitespace character is reached. Whitespace is detected using ft_isspace.

Parameters
inputThe string to search for whitespace.
iPointer to the current index, which will be updated in-place.
See also
skip_whitespace_ptr

◆ skip_whitespace_ptr()

char * skip_whitespace_ptr ( const char *  s)

#include <include/ft_string.h>

Returns a pointer to the first non-whitespace character.

Iterates through the string s and returns a pointer to the first character that is not a whitespace character. If all characters are whitespace, returns a pointer to the null-terminator.

Parameters
sThe string to scan.
Returns
A pointer to the first non-whitespace character, or to '\0'.
See also
skip_whitespace_index