Skip to content

Commit

Permalink
Add ft_padend and ft_padstart functions
Browse files Browse the repository at this point in the history
  • Loading branch information
GogoVega committed May 5, 2024
1 parent 78dbcc0 commit 6aa0ab9
Show file tree
Hide file tree
Showing 5 changed files with 240 additions and 1 deletion.
30 changes: 29 additions & 1 deletion include/libft_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
/* By: gdandele <gdandele@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/04/03 11:30:43 by gdandele #+# #+# */
/* Updated: 2024/05/01 14:55:34 by gdandele ### ########.fr */
/* Updated: 2024/05/05 16:00:08 by gdandele ### ########.fr */
/* */
/* ************************************************************************** */

Expand Down Expand Up @@ -102,6 +102,34 @@ void *ft_memmove(void *dst, const void *src, size_t len);
*/
void *ft_memset(void *s, int c, size_t n);

/**
* @brief Allocates (with malloc) and pads the start of a string with a
* specified character. It pads the start of the string `str` with the
* character `c` until the length of the string is equal to `len`.
* If the length of the string is already greater than or equal to `len`,
* the function returns the original string.
*
* @param str The string to be padded.
* @param c The character used for padding.
* @param len The desired length of the padded string.
* @return char* The padded string or NULL if the allocation fails.
*/
char *ft_padstart(char *str, char c, size_t len);

/**
* @brief Allocates (with malloc) and pads the end of a string with a
* specified character. It pads the end of the string `str` with the
* character `c` until the length of the string is equal to `len`.
* If the length of the string is already greater than or equal to `len`,
* the function returns the original string.
*
* @param str The string to be padded.
* @param c The character used for padding.
* @param len The desired length of the padded string.
* @return char* The padded string or NULL if the allocation fails.
*/
char *ft_padend(char *str, char c, size_t len);

/**
* @brief Allocates (with malloc) and returns an array of strings obtained by
* splitting ’s’ using the character ’c’ as a delimiter. The array must end
Expand Down
30 changes: 30 additions & 0 deletions src/string/ft_padend.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_padend.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gdandele <gdandele@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/05 15:17:34 by gdandele #+# #+# */
/* Updated: 2024/05/05 16:14:58 by gdandele ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

char *ft_padend(char *str, char c, size_t len)
{
char *dst;
size_t strlen;

strlen = ft_strlen(str);
if (strlen >= len)
return (ft_strdup(str));
dst = (char *) malloc(sizeof(char) * (len + 1));
if (!dst)
return (NULL);
ft_memset((dst + strlen), c, (len - strlen));
ft_memcpy(dst, str, strlen);
dst[len] = '\0';
return (dst);
}
30 changes: 30 additions & 0 deletions src/string/ft_padstart.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,30 @@
/* ************************************************************************** */
/* */
/* ::: :::::::: */
/* ft_padstart.c :+: :+: :+: */
/* +:+ +:+ +:+ */
/* By: gdandele <gdandele@student.s19.be> +#+ +:+ +#+ */
/* +#+#+#+#+#+ +#+ */
/* Created: 2024/05/05 15:32:12 by gdandele #+# #+# */
/* Updated: 2024/05/05 16:15:07 by gdandele ### ########.fr */
/* */
/* ************************************************************************** */

#include "libft.h"

char *ft_padstart(char *str, char c, size_t len)
{
char *dst;
size_t strlen;

strlen = ft_strlen(str);
if (strlen >= len)
return (ft_strdup(str));
dst = (char *) malloc(sizeof(char) * (len + 1));
if (!dst)
return (NULL);
ft_memset(dst, c, (len - strlen));
ft_memcpy((dst + (len - strlen)), str, strlen);
dst[len] = '\0';
return (dst);
}
68 changes: 68 additions & 0 deletions test/string/ft_padend_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#include "../test_helper.h"

int main()
{
int exit_code = 0;

// Test Case 1: Pad with '!' to length 10
char str1[] = "Hello";
char c1 = '!';
size_t len1 = 10;
char *result1 = ft_padend(str1, c1, len1);
if (result1 != NULL && strcmp(result1, "Hello!!!!!") == 0) {
success();
} else {
failed(&exit_code);
}
free(result1);

// Test Case 2: Pad with '0' to length 5
char str2[] = "123";
char c2 = '0';
size_t len2 = 5;
char *result2 = ft_padend(str2, c2, len2);
if (result2 != NULL && strcmp(result2, "12300") == 0) {
success();
} else {
failed(&exit_code);
}
free(result2);

// Test Case 3: Pad with ' ' to length 8
char str3[] = "Hello";
char c3 = ' ';
size_t len3 = 8;
char *result3 = ft_padend(str3, c3, len3);
if (result3 != NULL && strcmp(result3, "Hello ") == 0) {
success();
} else {
failed(&exit_code);
}
free(result3);

// Test Case 4: Pad with '\0' to length 3
char str4[] = "Hello";
char c4 = '\0';
size_t len4 = 3;
char *result4 = ft_padend(str4, c4, len4);
if (result4 != NULL && strcmp(result4, "Hello") == 0) {
success();
} else {
failed(&exit_code);
}
free(result4);

// Test Case 5: Pad with '$' to length 0
char str5[] = "";
char c5 = '$';
size_t len5 = 0;
char *result5 = ft_padend(str5, c5, len5);
if (result5 != NULL && strcmp(result5, "") == 0) {
success();
} else {
failed(&exit_code);
}
free(result5);

return (exit_code);
}
83 changes: 83 additions & 0 deletions test/string/ft_padstart_test.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,83 @@
#include "../test_helper.h"

int main()
{
int exit_code = 0;

// Test Case 1: Pad with '!' to length 10
char str1[] = "Hello";
char c1 = '!';
size_t len1 = 10;
char *result1 = ft_padstart(str1, c1, len1);
if (result1 != NULL && strcmp(result1, "!!!!!Hello") == 0)
{
success();
}
else
{
failed(&exit_code);
}
free(result1);

// Test Case 2: Pad with '0' to length 5
char str2[] = "123";
char c2 = '0';
size_t len2 = 5;
char *result2 = ft_padstart(str2, c2, len2);
if (result2 != NULL && strcmp(result2, "00123") == 0)
{
success();
}
else
{
failed(&exit_code);
}
free(result2);

// Test Case 3: Pad with ' ' to length 8
char str3[] = "Hello";
char c3 = ' ';
size_t len3 = 8;
char *result3 = ft_padstart(str3, c3, len3);
if (result3 != NULL && strcmp(result3, " Hello") == 0)
{
success();
}
else
{
failed(&exit_code);
}
free(result3);

// Test Case 4: Pad with '\0' to length 3
char str4[] = "Hello";
char c4 = '\0';
size_t len4 = 3;
char *result4 = ft_padstart(str4, c4, len4);
if (result4 != NULL && strcmp(result4, "Hello") == 0)
{
success();
}
else
{
failed(&exit_code);
}
free(result4);

// Test Case 5: Pad with '$' to length 0
char str5[] = "";
char c5 = '$';
size_t len5 = 0;
char *result5 = ft_padstart(str5, c5, len5);
if (result5 != NULL && strcmp(result5, "") == 0)
{
success();
}
else
{
failed(&exit_code);
}
free(result5);

return (exit_code);
}

0 comments on commit 6aa0ab9

Please sign in to comment.