✅strtrim
- Allocates (with malloc(3)) and returns a copy of
s1
with the characters specified in set
removed
from the beginning and the end of the string.
- 문자열에서
set
에 지정된 문자가 제거된 문자열 s1
의 사본을 반환.
s1
의 앞뒤에 set
에 있는 문자가 있으면 해당 문자들을 trim
한 후 리턴(뒤에서부터는 )
💾Prototype
char *ft_strtrim(char const *s1, char const *set);
💻Parameters
s1
: The string to be trimmed.
set
: The reference set of characters to trim.
💻Return value
The trimmed string
. NULL
if the allocation fails.
💾Code
#include "libft.h"
char* ft_strtrim(char const* s1, char const* set)
{
size_t front;
size_t rear;
char* str;
str = 0;
if (s1 != 0 && set != 0)
{
front = 0;
rear = ft_strlen(s1);
while (s1[front] && ft_strchr(set, s1[front]))
front++;
while (s1[rear - 1] && ft_strchr(set, s1[rear - 1]) && rear > front)
rear--;
str = (char*)malloc(sizeof(char) * (rear - front + 1));
if (str)
ft_strlcpy(str, &s1[front], rear - front + 1);
}
return (str);
}
💾Example
int main()
{
printf("%s\n", ft_strtrim("abqbc", "abc"));
printf("%s\n", ft_strtrim("xavocadoyz", "xyz"));
return 0;
}