Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -0,0 +1,35 @@
#include <stdio.h>
#include <stdlib.h>

int main()
{
char* string = NULL;
char* substring = NULL;
size_t stringSize = 0;
size_t substringSize = 0;

printf("Enter string: ");
int realStringSize = getline(&string, &stringSize, stdin);
printf("Enter substring: ");
int realSubstringSize = getline(&substring, &substringSize, stdin);

int substringCount = 0;
for (int i = 0; i < realStringSize - 1; i++) {
if (string[i] == substring[0]) {
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Если это такая оптимизация, то она почти бесполезна, лучше сделать ранний break во внутреннем цикле. Зато код будет гораздо читаемее.

substringCount++;
for (int j = 0; j < realSubstringSize - 1; j++) {
if (string[i + j] != substring[j]) {
substringCount--;
break;
}
}
}
}

printf("Substring count in string: %d \n", substringCount);

free(string);
free(substring);

return 0;
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
#include <stdio.h>

int main()
{
char character = 0;
int countNotClosedBrackets = 0;

printf("Enter string: ");
do {
scanf("%c", &character);

if (character == '(') {
countNotClosedBrackets++;
}
if (character == ')') {
countNotClosedBrackets--;
}

if (countNotClosedBrackets < 0) {
break;
}
} while (character != '\n');

if (countNotClosedBrackets == 0) {
printf("Brackets balanced. \n");
} else {
printf("Brackets not balanced. \n");
}
}