ft_strtrim

Subject

FT_STRTRIM (simplified)

NAME
    ft_strtrim -- trims character set from string
SYNOPSIS
    char *ft_strtrim(const char *s1, const char *set);
DESCRIPTION
    Allocate (with malloc(3)) and returns a copy of s1, without the characters specified in set at the beginning and the end of s1.
PARAMETERS
    s1: string to trim
    set: characters to trim
RETURN VALUES
    ft_strtrim() returns a trimmed copy of s1; NULL if the memory allocation failed.
AUTHORIZED EXTERNAL FUNCTIONS
    malloc(3)

Understandable explanation

The ft_strtrim() function takes a string and trims it.

What does trimming mean you might ask ? Let me explain.

Trimming means removing the characters specified in the set string from the start AND the end of the string s1, without removing the characters from the set that are in the middle of s1.

If we have the string ababaaaMy name is Simonbbaaabbad and our set is ab, we'll get this result out of the ft_strtrim() function : My name is Simon.

We removed every a and b from the start and the end of s1, without touching at the a in the middle of s1.

Hints

We have to remove characters from the start AND the end of s1, so why don't we just loop over the string, advance while we have a character to remove.

And then, do the same thing from the end of the string, leaving us with a start and an end index for our trimmed string.

That's basically how I did it, and how my code works.

Commented solution

ft_strtrim

Last updated

Was this helpful?