I am seeing a lot of answers for this problem in other languages but I am trying to find out a way to compare 2 version numbers given as strings. For example
I know I wont be able to tokenize 2 strings at once.
Fortunately, you do not need to: make a function that takes a string, and parses it for three integer numbers using strtok_r
(use a reentrant version, it's a lot safer).
strunct version_t {
int major;
int minor;
int build;
};
version_t parse_ver(const char* version_str) {
version_t res;
// Use strtok_r to split the string, and atoi to convert tokens to ints
return res;
}
Now you can call parse_ver
twice, get two version_t
values, and compare them side-by-side.
P.S. If you adopt a convention to always pad the numbers with leading zeros to a specific length, i.e. make sure that you write "141.1.03"
and not "141.1.3"
, you could substitute integer comparison with lexicographic one.