Website Summary Files Commits Branches @commit @diff @stats
path: root / pract_SI / Pract. 08 / Cstring_string
diff options
context:
space:
mode:
Commitf538738a9c75ca11464707cebecc49413e45df70 (patch)
Author
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-22 01:47:47 +0200
Committer
 Justsvetoslavov <justsvetoslavov@gmail.com>
2021-12-22 01:47:47 +0200
Filesc95694e5929b0ea1e29699380b8c105b91731290 /pract_SI/Pract. 08/Cstring_string
Introduction_to_programming_FMI-f538738a9c75ca11464707cebecc49413e45df70.tar
Introduction_to_programming_FMI-f538738a9c75ca11464707cebecc49413e45df70.tar.gz
Introduction_to_programming_FMI-f538738a9c75ca11464707cebecc49413e45df70.zip
Parentb5e964b1ca0f173ccde1965f2d747a756047153f (diff)
Add string tasks&solutions
diff --git a/pract_SI/Pract. 08/Cstring_string/README.md b/pract_SI/Pract. 08/Cstring_string/README.md
new file mode 100644
index 0000000..0e784ed
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/README.md
@@ -0,0 +1,29 @@
+## Задачи
+
+ 1.Напишете имплементация на strlen(str)
+*връща колко символа има от началото до ‘\0’*
+
+ 2.Напишете имплементация на strcmp(const char *str1, const char *str2)
+*сравнява 2 символни низа (> , = , <)*
+
+ 3.Напишете имплементация на strcat(destination,source)
+ *конкатенация*
+
+ 4.Напишете имплементация на strcpy(destination, source)
+ *копира source в destination*
+
+ 5.Напишете програма, която проверява дали дадена дума е палиндром
+
+ 6.Напишете програма, която приема 2 символни низа, които съдържат само малки букви('a' - 'z')
+ а)програмата да изведе на конзолата дължината на 2-та низа разделени с интервал
+ б)програмата да изведе на конзолата конкатенацията на 2-та низа.
+ в)програма да обърне низовете и да преобразува буквите им в главни и да изведе резултата на конзолата.
+
+ Вход:
+ abcd
+ ef
+
+ Изход:
+ 4 2
+ abcdef
+ DCBA FE
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strcat.cpp b/pract_SI/Pract. 08/Cstring_string/my_strcat.cpp
new file mode 100644
index 0000000..49d2d2e
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strcat.cpp
@@ -0,0 +1,12 @@
+//If destination has enough space
+void my_strcat(char* dest, const char* source)
+{
+ size_t destLength = my_strlen(dest);
+ size_t sourceLength = my_strlen(source);
+
+ for (size_t i = 0; i < sourceLength; i++)
+ {
+ dest[destLength + i] = source[i];
+ }
+ dest[destLength + sourceLength] = '\0';
+}
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strcat_returnArray.cpp b/pract_SI/Pract. 08/Cstring_string/my_strcat_returnArray.cpp
new file mode 100644
index 0000000..a06fffc
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strcat_returnArray.cpp
@@ -0,0 +1,45 @@
+//way 1
+const char* my_strcat(const char* dest, const char* source)
+{
+ size_t destLength = my_strlen(dest);
+ size_t sourceLength = my_strlen(source);
+ size_t resultLength = my_strlen(dest) + my_strlen(source) + 1;
+ char* result = new char[resultLength];
+
+ for (size_t i = 0; i < destLength; i++)
+ result[i] = dest[i];
+
+ for (size_t y = 0; y < sourceLength; y++)
+ result[destLength + y] = source[y];
+
+ result[resultLength - 1] = '\0';
+ return result;
+}
+
+//==================================================================
+//way 2
+const char* my_strcat(const char* dest, const char* source)
+{
+ size_t destLength = my_strlen(dest);
+ size_t sourceLength = my_strlen(source);
+ size_t resultLength = my_strlen(dest) + my_strlen(source) + 1;
+ char* result = new char[resultLength];
+
+ int index = 0;
+ while (dest[index] != '\0')
+ {
+ result[index] = dest[index];
+ index++;
+ }
+
+ index = 0;//reset
+
+ while (source[index] != '\0')
+ {
+ result[destLength + index] = source[index];
+ index++;
+ }
+
+ result[resultLength - 1] = '\0';
+ return result;
+}
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strcat_void.cpp b/pract_SI/Pract. 08/Cstring_string/my_strcat_void.cpp
new file mode 100644
index 0000000..3b0a501
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strcat_void.cpp
@@ -0,0 +1,41 @@
+unsigned my_strlen(const char *str) {
+ unsigned index = 0;
+
+ while (str[index] != TERMINATE_SYMBOL) {
+ index++;
+ }
+ return index;
+}
+
+//dest does NOT have enough space, return a new array
+void my_strcat4(char *&dest, const char* source)
+{
+ size_t destLength = my_strlen(dest);
+ size_t sourceLength = my_strlen(source);
+ size_t resultLength = destLength + sourceLength + 1;
+
+ char* temp = new char[destLength];
+ for (size_t i = 0; i < destLength; i++)
+ {
+ temp[i] = dest[i];
+ }
+
+ delete[] dest;
+ dest = nullptr;
+
+ dest = new char[resultLength];
+
+ for (size_t i = 0; i < destLength; i++)
+ {
+ dest[i] = temp[i];
+ }
+
+ for (size_t y = 0; y < sourceLength; y++)
+ {
+ dest[destLength + y] = source[y];
+ }
+
+ dest[resultLength - 1] = '\0';
+ delete[] temp;
+ temp = nullptr;
+}
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strcmp.cpp b/pract_SI/Pract. 08/Cstring_string/my_strcmp.cpp
new file mode 100644
index 0000000..1115dd0
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strcmp.cpp
@@ -0,0 +1,19 @@
+int min(const size_t first,const size_t second)
+{
+ return (first < second ? first : second);
+}
+
+int my_strcmp(const char* str1, const char* str2)
+{
+ size_t str1Length = my_strlen(str1);
+ size_t str2Length = my_strlen(str2);
+ size_t smallerLength = min(str1Length, str2Length);
+
+ for (size_t i = NULL; i < smallerLength; i++)
+ {
+ if (str1[i] - str2[i] != NULL)
+ return str1[i] - str2[i];
+ }
+
+ return str1Length - str2Length;
+} \ No newline at end of file
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strcpy.cpp b/pract_SI/Pract. 08/Cstring_string/my_strcpy.cpp
new file mode 100644
index 0000000..eb0b024
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strcpy.cpp
@@ -0,0 +1,10 @@
+void my_strcpy(char* dest, const char* source)
+{
+ size_t index = NULL;
+ while (source[index] != '\0')
+ {
+ dest[index] = source[index];
+ index++;
+ }
+ dest[index] = '\0';
+}
diff --git a/pract_SI/Pract. 08/Cstring_string/my_strlen.cpp b/pract_SI/Pract. 08/Cstring_string/my_strlen.cpp
new file mode 100644
index 0000000..332af71
--- /dev/null
+++ b/pract_SI/Pract. 08/Cstring_string/my_strlen.cpp
@@ -0,0 +1,7 @@
+size_t my_strlen(const char* str)
+{
+ size_t length = NULL ;
+ while (str[length] != '\0')
+ length++;
+ return length;
+} \ No newline at end of file