add new functions & tests

This commit is contained in:
dzruyk
2025-10-03 22:58:30 +03:00
parent 20c590f860
commit b16b98d7b7
5 changed files with 186 additions and 29 deletions

81
str.c
View File

@@ -6,6 +6,24 @@
#define INIT_SZ 20
//Внутренняя функция,
//позволяет убедиться что строка может вместить как минимум newsz символов
void
_str_ensure(str *s, int newsz)
{
int tmp;
newsz += 1; // добавляем единичку чтобы влез нулевой байт
if (newsz < s->capacity)
return;
tmp = s->capacity * 2;
if (tmp > newsz) {
newsz = tmp;
}
s->ptr = xrealloc(s->ptr, newsz);
s->capacity = newsz;
}
void
str_init(str *s)
{
@@ -36,6 +54,13 @@ str_data(str *s)
return NULL;
}
void
str_set(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
}
str
str_copy(str *s)
{
@@ -43,15 +68,61 @@ str_copy(str *s)
// <YOURCODE>
}
void
str_append(str *s, char *p)
int
str_count(str *s, char ch)
{
assert(s);
// <YOURCODE>
return 0;
}
void
str_append(str *s, char *cstr)
{
assert(s && cstr);
// <YOURCODE>
}
void
str_shrink(str *s, int sz)
str_append_n(str *s, char *cstr, int len)
{
assert(s && sz >= 0);
assert(s && cstr);
// <YOURCODE>
}
}
void
str_shrink(str *s, int len)
{
assert(s && len >= 0);
// <YOURCODE>
}
int
str_find(str *s, char *substr)
{
assert(s && substr);
char *p = strstr(s->ptr, substr);
if (p == NULL)
return -1;
return p - s->ptr;
}
str
str_sub(str *s, int start_idx, int length)
{
assert(s && start_idx >= 0 && length >= 0);
str s2;
str_init(&s2);
// <YOURCODE>
return s2;
}
// Hint: you can create temporary string object!
int
str_replace(str *s, char *substr, char *replacement)
{
assert(s && substr && replacement);
// <YOURCODE>
return 0;
}