diff options
added sem 12
4 files changed, 221 insertions, 0 deletions
| -rw-r--r-- | Sem. 12/README.md | 21 | ||||
| -rw-r--r-- | Sem. 12/task1.cpp | 54 | ||||
| -rw-r--r-- | Sem. 12/task3.cpp | 89 | ||||
| -rw-r--r-- | Sem. 12/tesk2.cpp | 57 |
diff --git a/Sem. 12/README.md b/Sem. 12/README.md new file mode 100644 index 0000000..dd05e07 --- /dev/null +++ b/Sem. 12/README.md @@ -0,0 +1,21 @@ +**Задача 1:** Напишете функция, която приема стринг и връща нов стринг, в който са премахнати всички цифри от първия стринг. +Новият стринг да е с точна дължина. + +Пример: +Вход: "ivan454sdd" + +Изход: "ivanssd" + +**Задача 2:** Да се реализира функция, която приема масив, в който всеки елемент се среща 2 пъти, с изключение на един елемент, който се среща точно веднъж. +Функцията да връща кой е този елемент. + + +**Задача 3:** Напишете функция, която примема стринг A, стринг X и приема и стринг Y и замества всяко срещане на X в А с Y. +Новият стринг да е с точна дължина. + +Пример: +A = hello friends! You are my best friend. I have no other friends! +X = friend +Y = enemy + +Резултат: hello enemys! You are my best enemy. I have no other enemys
\ No newline at end of file diff --git a/Sem. 12/task1.cpp b/Sem. 12/task1.cpp new file mode 100644 index 0000000..1698c33 --- /dev/null +++ b/Sem. 12/task1.cpp @@ -0,0 +1,54 @@ +#include <iostream> +using namespace std; + +bool isDigit(char ch) +{ + return ch >= '0' && ch <= '9'; +} +int countDigits(const char* str) +{ + //int len = strlen(str); + //int count = 0; + //for (int i = 0; i < len; i++) + //{ + // if (isDigit(str[i])) + // count++; + //} + + int count = 0; + while (*str != '\0') + { + if (isDigit(*str)) + count++; + str++; + } + return count; +} + +void removeDigits(const char* str, char* resultStr) +{ + while (*str != '\0') + { + if (!isDigit(*str)) + { + *resultStr = *str; + resultStr++; + } + str++; + } + *resultStr = '\0'; +} +int main() +{ + const char* str = "hello friends 123 , how are you? 343. Fine!"; + + int len = strlen(str) - countDigits(str); + + char* resultStr = new char[len + 1]; + + removeDigits(str, resultStr); + + cout << resultStr << endl; + + delete[] resultStr; +}
\ No newline at end of file diff --git a/Sem. 12/task3.cpp b/Sem. 12/task3.cpp new file mode 100644 index 0000000..ba0ba1b --- /dev/null +++ b/Sem. 12/task3.cpp @@ -0,0 +1,89 @@ +#include <iostream> +using namespace std; + +int search(const char* string, const char* searched); + +void replace(const char* string, const char* x, const char* y, char* result) +{ + int yLen = strlen(y); + int xLen = strlen(x); + while (true) + { + int indX = search(string, x); //14 + if (indX == -1) + break; + + + for (int i = 0; i < indX; i++) + result[i] = string[i]; + result += indX; + string += indX + xLen; + + for (int i = 0; i < yLen; i++) + result[i] = y[i]; + result += yLen; + } + + //the rest symbols + while (*string != '\0') + { + *result = *string; + result++; + string++; + } + *result = '\0'; + +} + +//return the first index of searched in str. +int search(const char* str, const char* searched) +{ + int len1 = strlen(str); + int len2 = strlen(searched); + for (int i = 0; i < len1 - len2+1; i++) + { + bool found = true; + for (int j = 0; j < len2; j++) + { + if (searched[j] != str[i + j]) + { + found = false; + break; + } + } + if (found) + return i; + } + return -1; +} +int countOccurrences(const char* str, const char* searched) +{ + int count = 0; + + int searchLen = strlen(searched); + int ind = search(str, searched); + while (ind != -1) + { + count++; + str += (ind + searchLen); + ind = search(str, searched); + } + return count; +} +int main() +{ + const char* str = "hello friends! You are my best friends. I have no other friends"; + const char* x = "friend"; + const char* y = "enemy"; + + int len = strlen(str); + int xOccurrences = countOccurrences(str, x); + + char* result = new char[len - xOccurrences*strlen(x) + xOccurrences*strlen(y) + 1]; + + replace(str, x, y, result); + + cout << result << endl; + + delete[] result; +}
\ No newline at end of file diff --git a/Sem. 12/tesk2.cpp b/Sem. 12/tesk2.cpp new file mode 100644 index 0000000..fc7df86 --- /dev/null +++ b/Sem. 12/tesk2.cpp @@ -0,0 +1,57 @@ +#include <iostream> +using namespace std; + + +bool isSymbol(char ch) +{ + return ('a' <= ch && ch <= 'z') || ('A' <= ch && ch <= 'Z'); +} +bool isVowel(char ch) +{ + switch (ch) + { + case 'A': + case 'O': + case 'I': + case 'U': + case 'E': + case 'a': + case 'o': + case 'i': + case 'u': + case 'e':return true; + } + return false; +} +void countVowelAndCons(const char* str, int& vowelCount, int& consCount) +{ + while (*str != '\0') + { + if (isSymbol(*str)) + { + if (isVowel(*str)) + vowelCount++; + else + consCount++; + } + str++; + } +} + +bool isBalanced(const char* str) +{ + int vowelCount = 0, consCount = 0; + + countVowelAndCons(str, vowelCount, consCount); + + return vowelCount == consCount; +} + +int main() +{ + const char* str = "TestMeau"; + + cout << isBalanced(str) << endl; + + +}
\ No newline at end of file |
