From 3d7b742eb8e9012e21a0516ad21fc7aefd043bf7 Mon Sep 17 00:00:00 2001 From: Syndamia Date: Tue, 11 Jan 2022 10:09:44 +0200 Subject: Added my work from todays up exam --- .../Programming introduction/Exams/Exam2/prob1.cpp | 34 ++++++++++++++++++++++ 1 file changed, 34 insertions(+) create mode 100644 Year 1/Programming introduction/Exams/Exam2/prob1.cpp (limited to 'Year 1/Programming introduction/Exams/Exam2/prob1.cpp') diff --git a/Year 1/Programming introduction/Exams/Exam2/prob1.cpp b/Year 1/Programming introduction/Exams/Exam2/prob1.cpp new file mode 100644 index 0000000..76d3971 --- /dev/null +++ b/Year 1/Programming introduction/Exams/Exam2/prob1.cpp @@ -0,0 +1,34 @@ +#include + +bool isAlphanumeric(char ch) { + return (ch >= '0' && ch <= '9') || (ch >= 'A' && ch <= 'Z') || (ch >= 'a' && ch <= 'z'); +} + +char* minWord(char *str) { + int startIndex = 0; + while (str[startIndex] != '\0' && !isAlphanumeric(str[startIndex])) + startIndex++; + + char *ptr = &str[startIndex]; + + for (int i = startIndex, wordStart = 0; str[i] != '\0'; i++) { + for (int j = i; isAlphanumeric(str[i]) && isAlphanumeric(ptr[wordStart]); i++, wordStart++) { + if (str[i] < ptr[wordStart]) + ptr = &str[j]; + else if (str[i] != ptr[wordStart]) + break; + } + wordStart = 0; + } + + return ptr; +} + +int main() { + char input[] = "me? why always me?"; + char *str = minWord(input); + + for (int i = 0; str[i] != '\0'; i++) + std::cout << str[i]; + std::cout << std::endl; +} -- cgit v1.3.1-26-gb76a