-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add
ft_padend
and ft_padstart
functions
- Loading branch information
Showing
5 changed files
with
240 additions
and
1 deletion.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | ||
} |