Website Summary Files Commits Branches @commit @diff @stats
path: root / Sem. 15 / task1.cpp
diff options
context:
space:
mode:
Commit8810168885f113c67c869d5aeb46e9412b5b073d (patch)
Author
 Angel Dimitriev <achi_d@abv.bg>
2021-01-16 01:26:44 +0200
Committer
 GitHub <noreply@github.com>
2021-01-16 01:26:44 +0200
Filesef999babef2cb6b51c99e1d9e320fc5d758a09e6 /Sem. 15/task1.cpp
Introduction_to_programming_FMI-8810168885f113c67c869d5aeb46e9412b5b073d.tar
Introduction_to_programming_FMI-8810168885f113c67c869d5aeb46e9412b5b073d.tar.gz
Introduction_to_programming_FMI-8810168885f113c67c869d5aeb46e9412b5b073d.zip
Parent27dfc3cef4061c873a00535f8b9937b9b03e60a2 (diff)
Create task1.cpp
1 files changed, 122 insertions, 0 deletions
-rw-r--r--Sem. 15/task1.cpp122
diff --git a/Sem. 15/task1.cpp b/Sem. 15/task1.cpp
new file mode 100644
index 0000000..f877253
--- /dev/null
+++ b/Sem. 15/task1.cpp
@@ -0,0 +1,122 @@
+#include <iostream>
+#include <vector>
+#include <queue>
+using namespace std;
+
+int countWords(const char* text)
+{
+ bool wasLastCharSpace = true;
+ int wordCount = 0;
+ int len = strlen(text);
+
+ for (int i = 0; i < len; i++)
+ {
+ if (wasLastCharSpace && text[i] != ' ' )
+ {
+ wordCount++;
+ wasLastCharSpace = false;
+ }
+ else if (text[i] == ' ')
+ wasLastCharSpace = true;
+ }
+ return wordCount;
+
+}
+int getWordLength(const char* text, int start)
+{
+ int len = 0;
+ int index = start;
+
+ while (text[index] != ' ' && text[index] != '\0')
+ {
+ index++;
+ len++;
+ }
+ return len;
+}
+
+void copyChars(char* dest, const char* text, int start, int len)
+{
+
+ for (int i = start, index = 0; index < len; i++, index++)
+ dest[index] = text[i];
+
+ dest[len] = '\0';
+}
+void fillWords(char** words, const char* text)
+{
+ int len = strlen(text);
+ int currentWordIndex = 0;
+
+ for (int i = 0; i < len; i++)
+ {
+ if (text[i] == ' ')
+ continue;
+
+ int currentWordLen = getWordLength(text, i);
+
+ words[currentWordIndex] = new char[currentWordLen + 1]; //(**)
+
+ copyChars(words[currentWordIndex], text, i, currentWordLen);
+
+ currentWordIndex++;
+ i += currentWordLen;
+ }
+}
+
+void swap(char** arr, int i, int j)
+{
+ char* wordPtr = arr[i];
+ arr[i] = arr[j];
+ arr[j] = wordPtr;
+}
+void selectionSort(char** arr, int len)
+{
+ for (int pos = 0; pos < len - 1; pos++)
+ {
+ int minIndex = pos;
+ for (int i = pos; i < len; i++)
+ {
+ if (strcmp(arr[i], arr[minIndex]) < 0)
+ minIndex = i;
+ }
+ if (pos != minIndex)
+ swap(arr, pos, minIndex);
+ }
+}
+
+void free(char** words, int wordsCount)
+{
+ for (int i = 0; i < wordsCount; i++)
+ delete[] words[i]; // (**)
+ delete[] words; // (*)
+}
+void task1(const char* text)
+{
+
+ int wordsCount = countWords(text);
+
+ char** words = new char*[wordsCount]; // (*)
+
+ fillWords(words, text);
+
+ cout << "Before sort:" << endl;
+ for (int i = 0; i < wordsCount; i++)
+ cout << words[i] << endl;
+
+ selectionSort(words, wordsCount);
+
+ cout << "After sort:" << endl;
+ for (int i = 0; i < wordsCount; i++)
+ cout << words[i] << endl;
+
+ free(words);
+}
+
+int main()
+{
+ const char* text = " hello friends. how are you friends. ";
+
+ task1(text);
+
+}