Вторая лаба
This commit is contained in:
171
str.c
171
str.c
@@ -1,112 +1,198 @@
|
||||
#include <assert.h>
|
||||
#include <string.h>
|
||||
#include <stdlib.h>
|
||||
|
||||
#include "str.h"
|
||||
#include "util.h"
|
||||
|
||||
#define INIT_SZ 20
|
||||
|
||||
//Внутренняя функция,
|
||||
//позволяет убедиться что строка может вместить как минимум newsz символов
|
||||
// -----------------------------------------------------------
|
||||
// INTERNAL: ensure capacity >= newsz (+1 for '\0')
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
_str_ensure(str *s, int newsz)
|
||||
{
|
||||
int tmp;
|
||||
newsz += 1; // добавляем единичку чтобы влез нулевой байт
|
||||
|
||||
newsz += 1; // Account for null terminator
|
||||
|
||||
if (newsz < s->capacity)
|
||||
return;
|
||||
|
||||
tmp = s->capacity * 2;
|
||||
if (tmp > newsz) {
|
||||
if (tmp > newsz)
|
||||
newsz = tmp;
|
||||
}
|
||||
|
||||
s->ptr = xrealloc(s->ptr, newsz);
|
||||
s->capacity = newsz;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Initialize empty string
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_init(str *s)
|
||||
{
|
||||
assert(s);
|
||||
// <YOURCODE>
|
||||
|
||||
s->capacity = INIT_SZ;
|
||||
s->len = 0;
|
||||
s->ptr = xmalloc(s->capacity);
|
||||
s->ptr[0] = '\0';
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Free memory
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_deinit(str *s)
|
||||
{
|
||||
if (s->ptr)
|
||||
free(s->ptr);
|
||||
|
||||
s->ptr = NULL;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Initialize with provided C-string
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_init_data(str *s, const char *initial)
|
||||
{
|
||||
assert(s && initial);
|
||||
// <YOURCODE>
|
||||
|
||||
int n = strlen(initial);
|
||||
s->capacity = (n + 1 < INIT_SZ) ? INIT_SZ : n + 1;
|
||||
s->len = n;
|
||||
|
||||
s->ptr = xmalloc(s->capacity);
|
||||
memcpy(s->ptr, initial, n + 1); // copy including '\0'
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Return pointer to underlying C-string
|
||||
// -----------------------------------------------------------
|
||||
char *
|
||||
str_data(str *s)
|
||||
{
|
||||
assert(s);
|
||||
// <YOURCODE>
|
||||
return NULL;
|
||||
return s->ptr;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Replace contents with provided C-string
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_set(str *s, char *cstr)
|
||||
{
|
||||
assert(s && cstr);
|
||||
// <YOURCODE>
|
||||
|
||||
int n = strlen(cstr);
|
||||
_str_ensure(s, n);
|
||||
|
||||
memcpy(s->ptr, cstr, n + 1); // include '\0'
|
||||
s->len = n;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Create deep copy of string
|
||||
// -----------------------------------------------------------
|
||||
str
|
||||
str_copy(str *s)
|
||||
{
|
||||
assert(s);
|
||||
// <YOURCODE>
|
||||
|
||||
str s2;
|
||||
str_init(&s2);
|
||||
|
||||
_str_ensure(&s2, s->len);
|
||||
memcpy(s2.ptr, s->ptr, s->len + 1);
|
||||
s2.len = s->len;
|
||||
|
||||
return s2;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Count occurrences of character
|
||||
// -----------------------------------------------------------
|
||||
int
|
||||
str_count(str *s, char ch)
|
||||
{
|
||||
assert(s);
|
||||
// <YOURCODE>
|
||||
return 0;
|
||||
|
||||
int cnt = 0;
|
||||
for (int i = 0; i < s->len; i++)
|
||||
if (s->ptr[i] == ch)
|
||||
cnt++;
|
||||
|
||||
return cnt;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Append C-string
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_append(str *s, char *cstr)
|
||||
{
|
||||
assert(s && cstr);
|
||||
// <YOURCODE>
|
||||
|
||||
int n = strlen(cstr);
|
||||
_str_ensure(s, s->len + n);
|
||||
|
||||
memcpy(s->ptr + s->len, cstr, n + 1);
|
||||
s->len += n;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Append exactly len bytes from cstr
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_append_n(str *s, char *cstr, int len)
|
||||
{
|
||||
assert(s && cstr);
|
||||
// <YOURCODE>
|
||||
|
||||
_str_ensure(s, s->len + len);
|
||||
|
||||
memcpy(s->ptr + s->len, cstr, len);
|
||||
s->len += len;
|
||||
|
||||
s->ptr[s->len] = '\0'; // add terminator
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Shrink string to new length
|
||||
// -----------------------------------------------------------
|
||||
void
|
||||
str_shrink(str *s, int len)
|
||||
{
|
||||
assert(s && len >= 0);
|
||||
// <YOURCODE>
|
||||
|
||||
if (len > s->len)
|
||||
len = s->len;
|
||||
|
||||
s->len = len;
|
||||
s->ptr[len] = '\0';
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Find substring, return index or -1
|
||||
// -----------------------------------------------------------
|
||||
int
|
||||
str_find(str *s, char *substr)
|
||||
{
|
||||
assert(s && substr);
|
||||
|
||||
char *p = strstr(s->ptr, substr);
|
||||
if (p == NULL)
|
||||
if (!p)
|
||||
return -1;
|
||||
return p - s->ptr;
|
||||
|
||||
return (int)(p - s->ptr);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------
|
||||
// Extract substring [start_idx, start_idx+length)
|
||||
// -----------------------------------------------------------
|
||||
str
|
||||
str_sub(str *s, int start_idx, int length)
|
||||
{
|
||||
@@ -114,15 +200,58 @@ str_sub(str *s, int start_idx, int length)
|
||||
|
||||
str s2;
|
||||
str_init(&s2);
|
||||
// <YOURCODE>
|
||||
|
||||
if (start_idx > s->len)
|
||||
return s2; // empty
|
||||
|
||||
if (start_idx + length > s->len)
|
||||
length = s->len - start_idx;
|
||||
|
||||
_str_ensure(&s2, length);
|
||||
|
||||
memcpy(s2.ptr, s->ptr + start_idx, length);
|
||||
s2.ptr[length] = '\0';
|
||||
s2.len = length;
|
||||
|
||||
return s2;
|
||||
}
|
||||
|
||||
// Hint: you can create temporary string object!
|
||||
// -----------------------------------------------------------
|
||||
// Replace all occurrences of substr with replacement
|
||||
// -----------------------------------------------------------
|
||||
int
|
||||
str_replace(str *s, char *substr, char *replacement)
|
||||
{
|
||||
assert(s && substr && replacement);
|
||||
// <YOURCODE>
|
||||
|
||||
int sub_len = strlen(substr);
|
||||
int rep_len = strlen(replacement);
|
||||
|
||||
if (sub_len == 0)
|
||||
return 0;
|
||||
|
||||
str result;
|
||||
str_init(&result);
|
||||
|
||||
char *pos = s->ptr;
|
||||
char *found;
|
||||
int count = 0;
|
||||
|
||||
while ((found = strstr(pos, substr)) != NULL) {
|
||||
int prefix_len = (int)(found - pos);
|
||||
|
||||
str_append_n(&result, pos, prefix_len);
|
||||
str_append(&result, replacement);
|
||||
|
||||
pos = found + sub_len;
|
||||
count++;
|
||||
}
|
||||
|
||||
if (*pos)
|
||||
str_append(&result, pos);
|
||||
|
||||
str_set(s, result.ptr);
|
||||
str_deinit(&result);
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
31
wcl.c
Normal file
31
wcl.c
Normal file
@@ -0,0 +1,31 @@
|
||||
#include <stdio.h>
|
||||
|
||||
int main(int argc, char *argv[]) {
|
||||
if (argc < 2) {
|
||||
printf("Использование: %s <путь_к_файлу>\n", argv[0]);
|
||||
return 1;
|
||||
}
|
||||
|
||||
for (int i = 1; i < argc; i++) {
|
||||
FILE *file = fopen(argv[i], "r");
|
||||
if (file == NULL) {
|
||||
printf("Ошибка: не удалось открыть файл '%s'\n", argv[1]);
|
||||
continue;
|
||||
}
|
||||
int line_count = 0;
|
||||
int ch;
|
||||
|
||||
while ((ch = fgetc(file)) != EOF) {
|
||||
if (ch == '\n') {
|
||||
line_count++;
|
||||
}
|
||||
}
|
||||
|
||||
printf("Строк: %d\t%s\n", line_count, argv[i]);
|
||||
|
||||
fclose(file);
|
||||
}
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user