Simvik

Compare version strings

Order dotted version strings by their numbers, not their characters. An easy embedded C problem (Git and releases for firmware), run and graded in the browser.

easy · Git and releases for firmware · versioning · strings · parsing

An update server and a bootloader both need to know whether one firmware version is newer than another. Write ver_cmp(a, b) for dotted version strings such as "2.10.0": return -1 if a is older, 0 if they are equal, 1 if a is newer.

  • Compare field by field as numbers: 2.10 is newer than 2.9.
  • A missing field counts as 0, so "1.2" equals "1.2.0".
  • The strings contain only digits and dots, with at most 4 fields.

Starter code

int ver_cmp(const char *a, const char *b) {
  int r = strcmp(a, b);
  return r < 0 ? -1 : r > 0 ? 1 : 0;
}

Example tests

Two-digit fields and missing fields

show("1.10.0", "1.9.3");
show("2.0.0", "2.0.0");
show("1.2", "1.2.0");

Expected output:

1.10.0 > 1.9.3
2.0.0 = 2.0.0
1.2 = 1.2.0

Learn it first

More Git and releases for firmware problems