1
1
mirror of https://github.com/theoludwig/libcproject.git synced 2024-11-13 23:43:13 +01:00

feat: add string_remove_character

This commit is contained in:
Théo LUDWIG 2023-08-09 20:21:33 +02:00
parent f0716c2e12
commit 6505e3ba49
Signed by: theoludwig
GPG Key ID: ADFE5A563D718F3B
4 changed files with 35 additions and 0 deletions

View File

@ -37,6 +37,20 @@ void string_replace(string_t string, char search, char replace) {
string[string_length] = '\0'; string[string_length] = '\0';
} }
void string_remove_character(char* string, char search) {
size_t string_length = string_get_length(string);
for (size_t index = 0; index < string_length; index++) {
if (string[index] == search) {
for (size_t index_string = index; index_string < string_length; index_string++) {
string[index_string] = string[index_string + 1];
}
string_length--;
index--;
}
}
string[string_length] = '\0';
}
void string_trim_start(string_t string, char character) { void string_trim_start(string_t string, char character) {
size_t string_length = string_get_length(string); size_t string_length = string_get_length(string);
size_t index_space = 0; size_t index_space = 0;

View File

@ -51,6 +51,17 @@ void string_to_lowercase(string_t string);
*/ */
void string_replace(string_t string, char search, char replace); void string_replace(string_t string, char search, char replace);
/**
* @brief Removes all the occurrences of a character in a string.
*
* NOTE: Mutates the string.
*
* @param string
* @param search A character search value.
* @since v4.1.0
*/
void string_remove_character(char* string, char search);
/** /**
* @brief Removes all `character` from the start of a string. * @brief Removes all `character` from the start of a string.
* *

View File

@ -5,6 +5,7 @@ void string_test() {
string_to_uppercase_test(); string_to_uppercase_test();
string_to_lowercase_test(); string_to_lowercase_test();
string_replace_test(); string_replace_test();
string_remove_character_test();
string_trim_start_test(); string_trim_start_test();
string_trim_end_test(); string_trim_end_test();
string_trim_test(); string_trim_test();
@ -54,6 +55,13 @@ void string_replace_test() {
free(string); free(string);
} }
void string_remove_character_test() {
string_t string = string_copy("hello world");
string_remove_character(string, 'l');
assert(assert_string_equal(string, "heo word"));
free(string);
}
void string_trim_start_test() { void string_trim_start_test() {
string_t string = string_copy(" hello world "); string_t string = string_copy(" hello world ");
string_trim_start(string, ' '); string_trim_start(string, ' ');

View File

@ -18,6 +18,8 @@ void string_to_lowercase_test();
void string_replace_test(); void string_replace_test();
void string_remove_character_test();
void string_trim_start_test(); void string_trim_start_test();
void string_trim_end_test(); void string_trim_end_test();