axl_strrev
This commit is contained in:
parent
c341e973b1
commit
e32beb707c
3 changed files with 130 additions and 0 deletions
22
axl_string.c
22
axl_string.c
|
|
@ -164,3 +164,25 @@ i8* axl_strstr(const i8* str, const i8* substr)
|
|||
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i8* axl_strrev(i8* str)
|
||||
{
|
||||
if(!str)
|
||||
{
|
||||
return NULL;
|
||||
}
|
||||
|
||||
i8* start = str;
|
||||
i8* end = str + axl_strlen(str) - 1;
|
||||
|
||||
while(start < end)
|
||||
{
|
||||
i8 tmp = *start;
|
||||
*start = *end;
|
||||
*end = tmp;
|
||||
start++;
|
||||
end--;
|
||||
}
|
||||
|
||||
return str;
|
||||
}
|
||||
|
|
|
|||
|
|
@ -11,5 +11,6 @@ i32 axl_strcmp(const i8* s1, const i8* s2);
|
|||
i32 axl_strncmp(const i8* s1, const i8* s2, u32 n);
|
||||
const i8* axl_strchr(const i8* str, i8 c);
|
||||
i8* axl_strstr(const i8* str, const i8* substr);
|
||||
i8* axl_strrev(i8* str);
|
||||
|
||||
#endif // AXL_STRING
|
||||
|
|
|
|||
|
|
@ -440,6 +440,113 @@ KOAN(strstr_special_chars)
|
|||
ASSERT_PTR_EQ(str + 11, axl_strstr(str, "\n"));
|
||||
}
|
||||
|
||||
KOAN(strrev_null)
|
||||
{
|
||||
ASSERT_NULL(axl_strrev(NULL));
|
||||
}
|
||||
|
||||
KOAN(strrev_empty)
|
||||
{
|
||||
i8 str[] = "";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_single_char)
|
||||
{
|
||||
i8 str[] = "a";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("a", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_two_chars)
|
||||
{
|
||||
i8 str[] = "ab";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("ba", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_odd_length)
|
||||
{
|
||||
i8 str[] = "hello";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("olleh", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_even_length)
|
||||
{
|
||||
i8 str[] = "world";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("dlrow", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_palindrome)
|
||||
{
|
||||
i8 str[] = "racecar";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("racecar", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_with_spaces)
|
||||
{
|
||||
i8 str[] = "hello world";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("dlrow olleh", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_with_special_chars)
|
||||
{
|
||||
i8 str[] = "a\nb\tc";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("c\tb\na", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_numbers)
|
||||
{
|
||||
i8 str[] = "123456789";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("987654321", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_mixed)
|
||||
{
|
||||
i8 str[] = "a1b2c3";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("3c2b1a", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_long_string)
|
||||
{
|
||||
i8 str[] = "abcdefghijklmnopqrstuvwxyz";
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(str, result);
|
||||
ASSERT_STR_EQ("zyxwvutsrqponmlkjihgfedcba", str);
|
||||
}
|
||||
|
||||
KOAN(strrev_in_place)
|
||||
{
|
||||
i8 str[] = "test";
|
||||
i8* original_ptr = str;
|
||||
i8* result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(original_ptr, result);
|
||||
ASSERT_STR_EQ("tset", str);
|
||||
|
||||
// Reverse again to get back original
|
||||
result = axl_strrev(str);
|
||||
ASSERT_PTR_EQ(original_ptr, result);
|
||||
ASSERT_STR_EQ("test", str);
|
||||
}
|
||||
|
||||
int main(void)
|
||||
{
|
||||
axl_init();
|
||||
|
|
|
|||
Loading…
Reference in a new issue