Skip to main content
Tweeted twitter.com/StackCodeReview/status/1497224884329529388
spellings and code fence
Source Link
Toby Speight
  • 81.8k
  • 14
  • 101
  • 308

Compare two version numbernumbers

Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer valuevalues ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.



int compareVersion(const char * v1, const char * v2)
{
    while (*v1 || * v2) 
    {
        int a=0, b=0;
        
        while (*v1 && *v1 != '.')  a = 10*a + (*v1++ - '0');
        while (*v2 && *v2 != '.')  b = 10*b + (*v2++ - '0');
        if (*v1) v1++;
        if (*v2) v2++;
        if (a < b) return -1;
        if (a > b) return +1;
    }
    return 0;
}
```

Compare version number

Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.



int compareVersion(const char * v1, const char * v2)
{
    while (*v1 || * v2) 
    {
        int a=0, b=0;
        
        while (*v1 && *v1 != '.')  a = 10*a + (*v1++ - '0');
        while (*v2 && *v2 != '.')  b = 10*b + (*v2++ - '0');
        if (*v1) v1++;
        if (*v2) v2++;
        if (a < b) return -1;
        if (a > b) return +1;
    }
    return 0;
}
```

Compare two version numbers

Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer values ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.



int compareVersion(const char * v1, const char * v2)
{
    while (*v1 || * v2) 
    {
        int a=0, b=0;
        
        while (*v1 && *v1 != '.')  a = 10*a + (*v1++ - '0');
        while (*v2 && *v2 != '.')  b = 10*b + (*v2++ - '0');
        if (*v1) v1++;
        if (*v2) v2++;
        if (a < b) return -1;
        if (a > b) return +1;
    }
    return 0;
}
Source Link

Compare version number

Given two version numbers, version1 and version2, compare them.

Version numbers consist of one or more revisions joined by a dot '.'. Each revision consists of digits and may contain leading zeros. Every revision contains at least one character.

To compare version numbers, compare their revisions in left-to-right order. Revisions are compared using their integer value ignoring any leading zeros. This means that revisions 1 and 001 are considered equal. If a version number does not specify a revision at an index, then treat the revision as 0. For example, version 1.0 is less than version 1.1 because their revision 0s are the same, but their revision 1s are 0 and 1 respectively, and 0 < 1.



int compareVersion(const char * v1, const char * v2)
{
    while (*v1 || * v2) 
    {
        int a=0, b=0;
        
        while (*v1 && *v1 != '.')  a = 10*a + (*v1++ - '0');
        while (*v2 && *v2 != '.')  b = 10*b + (*v2++ - '0');
        if (*v1) v1++;
        if (*v2) v2++;
        if (a < b) return -1;
        if (a > b) return +1;
    }
    return 0;
}
```