Summary Files Commits Branches @commit @diff @stats
path: root / Year 1 / Programming introduction / Exams / Exam2 / prob1.cpp
blob: 76d3971a17d52d73639d0e824896edd04dc8be6a (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
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;
}