diff --git a/src/29sep25/braces.c b/src/29sep25/braces.c new file mode 100644 index 0000000..e4640ba --- /dev/null +++ b/src/29sep25/braces.c @@ -0,0 +1,49 @@ +#include +#include +#include +#include + +int checkStr(char* string) +{ + int balance = 0; + int len = strlen(string); + for (int i = 0; i < len; i++) { + if (string[i] == '(') + balance += 1; + else if (string[i] == ')') + balance -= 1; + if (balance < 0) + return 0; + }; + if (balance == 0) + return true; + else + return false; +} + +int main(void) +{ + char* string = NULL; + int capacity = 10; + int size = 0; + int c; + + string = malloc(capacity); + + printf("Введите строку: "); + + while ((c = getchar()) != '\n') { + if (size >= capacity - 1) { + capacity *= 2; + char* temp = realloc(string, capacity); + string = temp; + } + string[size++] = c; + } + string[size] = '\0'; + + bool correct = checkStr(string); + printf("%s\n", correct ? "true" : "false"); + + return 0; +} \ No newline at end of file diff --git a/src/29sep25/count.c b/src/29sep25/count.c new file mode 100644 index 0000000..d1a26ef --- /dev/null +++ b/src/29sep25/count.c @@ -0,0 +1,38 @@ +#include +#include + +int count(char *str, char *str1) +{ + int size = strlen(str); + int size1 = strlen(str1); + int number_of_substrings = 0; + + if (size == 0 || size1 == 0) + return 0; + + for (int start = 0; start <= size - size1; start++) { + /* если элемент совпал, продожаем считать, иначе ищем следующий стартовы элемент */ + int coincidences = 0; + for (int current = 0; current < size1; current++) { + if (str1[current] == str[start + current]) + coincidences += 1; + else + break; + } + + /* если все элементы подстроки есть, то добавляем одно вхождение */ + if (coincidences == size1) + number_of_substrings += 1; + } + + return number_of_substrings; +} + +int main() +{ + char string[7] = "abcabc"; + char string1[3] = "ab"; + int result = count(string, string1); + printf("Количество вхождений %d\n", result); + return 0; +} \ No newline at end of file diff --git a/src/29sep25/zero_elements.c b/src/29sep25/zero_elements.c new file mode 100644 index 0000000..24ee07b --- /dev/null +++ b/src/29sep25/zero_elements.c @@ -0,0 +1,36 @@ +#include + +int count_zero_elements(int arr[], int size) +{ + int count = 0; + + for (int i = 0; i < size; i++) { + if (arr[i] == 0) + count++; + } + + return count; +} + +/* в C приходится выводить массив по одному элементу */ +void print_array(int arr[], int size) +{ + printf("Массив: ["); + for (int i = 0; i < size; i++) { + printf("%d", arr[i]); + if (i < size - 1) + printf(", "); + } + printf("]\n"); +} + +int main() +{ + int arr[] = {1, 0, 3, 0, 5, 0, 7}; + int size = sizeof(arr) / sizeof(arr[0]); + print_array(arr, size); + int zeros = count_zero_elements(arr, size); + printf("Количество нулевых элементов: %d\n", zeros); + + return 0; +}