diff options
Added my work from todays up exam
1 files changed, 34 insertions, 0 deletions
| -rw-r--r-- | Year 1/Programming introduction/Exams/Exam2/prob1.cpp | 34 |
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 <iostream> + +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; +} |
