diff --git a/.vscode/tasks.json b/.vscode/tasks.json new file mode 100644 index 0000000..08d9005 --- /dev/null +++ b/.vscode/tasks.json @@ -0,0 +1,28 @@ +{ + "tasks": [ + { + "type": "cppbuild", + "label": "C/C++: gcc build active file", + "command": "/usr/bin/gcc", + "args": [ + "-fdiagnostics-color=always", + "-g", + "${file}", + "-o", + "${fileDirname}/${fileBasenameNoExtension}" + ], + "options": { + "cwd": "${fileDirname}" + }, + "problemMatcher": [ + "$gcc" + ], + "group": { + "kind": "build", + "isDefault": true + }, + "detail": "Task generated by Debugger." + } + ], + "version": "2.0.0" +} \ No newline at end of file diff --git a/str.c b/str.c index 8036bd5..29d1205 100644 --- a/str.c +++ b/str.c @@ -6,123 +6,184 @@ #define INIT_SZ 20 -//Внутренняя функция, -//позволяет убедиться что строка может вместить как минимум newsz символов void _str_ensure(str *s, int newsz) { - int tmp; - newsz += 1; // добавляем единичку чтобы влез нулевой байт - if (newsz < s->capacity) - return; + 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; + tmp = s->capacity * 2; + if (tmp > newsz) { + newsz = tmp; + } + s->ptr = xrealloc(s->ptr, newsz); + s->capacity = newsz; } void str_init(str *s) { - assert(s); - // + assert(s); + s->len = 0; + s->capacity = INIT_SZ; + s->ptr = xmalloc(INIT_SZ); + s->ptr[0] = '\0'; } void str_deinit(str *s) { - if (s->ptr) - free(s->ptr); - s->ptr = NULL; + if (s->ptr) + free(s->ptr); + s->ptr = NULL; + s->len = 0; + s->capacity = 0; } void str_init_data(str *s, const char *initial) { - assert(s && initial); - // + assert(s && initial); + int len = strlen(initial); + s->len = len; + s->capacity = len + 1; + s->ptr = xmalloc(len + 1); + strcpy(s->ptr, initial); } char * str_data(str *s) { - assert(s); - // - return NULL; + assert(s); + return s->ptr; } void str_set(str *s, char *cstr) { - assert(s && cstr); - // + assert(s && cstr); + int len = strlen(cstr); + _str_ensure(s, len); + strcpy(s->ptr, cstr); + s->len = len; } str str_copy(str *s) { - assert(s); - // + assert(s); + str s2; + str_init_data(&s2, s->ptr); + return s2; } - + int str_count(str *s, char ch) { - assert(s); - // - return 0; + assert(s); + int count = 0; + for (int i = 0; i < s->len; i++) + { + if (s->ptr[i] == ch) + {count++;} + } + return count; } void str_append(str *s, char *cstr) { - assert(s && cstr); - // + assert(s && cstr); + str_append_n(s, cstr, strlen(cstr)); } void str_append_n(str *s, char *cstr, int len) { - assert(s && cstr); - // + assert(s && cstr); + _str_ensure(s, s->len + len); + memcpy(s->ptr + s->len, cstr, len); + s->len += len; + s->ptr[s->len] = '\0'; } void str_shrink(str *s, int len) { - assert(s && len >= 0); - // + assert(s && len >= 0); + if (len > s->len) + {return;} + s->len = len; + s->ptr[s->len] = '\0'; } 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; + 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); + assert(s && start_idx >= 0 && length >= 0); - str s2; - str_init(&s2); - // - return s2; + str s2; + str_init(&s2); + + if (start_idx >= s->len) + {return s2;} + + int true_length = length; + if (start_idx + length > s->len) + {true_length = s->len - start_idx;} + + _str_ensure(&s2, true_length); + memcpy(s2.ptr, s->ptr + start_idx, true_length); + s2.len = true_length; + s2.ptr[true_length] = '\0'; + + return s2; } -// Hint: you can create temporary string object! int str_replace(str *s, char *substr, char *replacement) { - assert(s && substr && replacement); - // - return 0; -} + assert(s && substr && replacement); + + int count = 0; + str replaced; + str_init(&replaced); + + char *current = s->ptr; + char *found; + int substr_len = strlen(substr); + int replacement_len = strlen(replacement); + + while ((found = strstr(current, substr)) != NULL) + { + int prefix_len = found - current; + if (prefix_len > 0) + {str_append_n(&replaced, current, prefix_len);} + + str_append_n(&replaced, replacement, replacement_len); + + current = found + substr_len; + count++; + } + + if (*current != '\0') { + str_append(&replaced, current); + } + + str_set(s, replaced.ptr); + str_deinit(&replaced); + + return count; +} \ No newline at end of file diff --git a/str_test b/str_test new file mode 100755 index 0000000..ae9211a Binary files /dev/null and b/str_test differ diff --git a/test/test1 b/test/test1 new file mode 100644 index 0000000..1e12a24 --- /dev/null +++ b/test/test1 @@ -0,0 +1,6 @@ +sdsdad adadas +adasdasdsd +adasdadad +adadadadas +adadadadad +asdasdadasd diff --git a/test/test2 b/test/test2 new file mode 100644 index 0000000..e69de29 diff --git a/wcl b/wcl new file mode 100755 index 0000000..0016b05 Binary files /dev/null and b/wcl differ diff --git a/wcl.c b/wcl.c new file mode 100644 index 0000000..dde7760 --- /dev/null +++ b/wcl.c @@ -0,0 +1,65 @@ +#include +#include +int countLines(const char* fname); +int countBytes (const char* fname); +int countWords (const char* fname); + +int main(int argc, char *argv[]) +{ + int nlines = 0; + int nbytes = 0; + int nwords = 0; + for(int i=1; i < argc; i++) + { + nlines = countLines(argv[i]); + nbytes = countBytes(argv[i]); + nwords = countWords(argv[i]); + printf("%s: lines - %d, bytes - %d, words - %d\n", argv[i], nlines, nbytes, nwords); + } + return 0; +} + +int countLines(const char* fname) +{ + FILE* file = fopen(fname, "r"); + int lines = 0; + char buffer[1024]; + while (fgets(buffer, sizeof(buffer), file) != NULL) + {lines++;} + fclose(file); + return lines; +} + +int countBytes (const char* fname) +{ + FILE* file = fopen(fname, "r"); + int bytes = 0; + int c; + while ((c = fgetc(file)) != EOF) + { + bytes++; + } + return bytes; +} + +int countWords (const char* fname) +{ + FILE* file = fopen(fname, "r"); + int words = 0; + int c; + int is_in_word = 0; + while ((c = fgetc(file)) != EOF) + { + if (isspace(c)) + {is_in_word = 0;} + else + { + if (is_in_word == 0) + { + words++; + is_in_word = 1; + } + } + } + return words; +}