Strncmp Function In C

In C programming, the 'strncmp' function is utilized for comparing two strings character by character, up to a defined length.

Syntax

The syntax for executing the strncmp function is demonstrated below:

Example

int strncmp(char *s1,char *s2, int n);

The trio of parameters consist of:

  • 's1' and 's2' represent references to the two strings intended for a comparison operation.
  • 'n' specifies the quantity of characters to be evaluated when comparing the two strings.

Based on the comparisons of both the strings, the function returns an integer , based on the following conditions.

  • The characteristic compares strings s1 and s2 with the usage of specific encodings, including ASCII , and returns a negative integer if string s1 is less than the string s2 .
  • When comparing two strings, the method returns a positive integer if s1 is obviously greater than s2 .
  • While the comparison of both the strings upto the specified number of characters, if s1 equals s2 , the function returns the value of 0 .
  • Example:

A code excerpt demonstrating the utilization of the strncmp function:

Example

#include <stdio.h>
#include <string.h>
int main() {
    char s1[] = "javaLogic Practice";
    char s2[] = "LOGIC PRACTICE";
    int length= 5;
    int res = strncmp(s1, s2, length);
    if (res< 0) {
        printf(''The string '%s' is less than the string  '%s' up to the first %d characters.\n", s1, s2,length);
    }
    else if (res> 0) {
        printf("The string '%s' is greater than the string '%s' up to the first %d characters.\n", s1, s2,length);
    }
    else {
printf("The string '%s' and the string '%s' are equal up to the first %d characters.\n", s1, s2,length);}
    return 0;
}

Output:

Output

The string 'javaLogic Practice' is greater than the string 'LOGIC PRACTICE' up to the first 5 characters.

Some other points regarding the strncmp function C:

  • Case-sensitive: In case-sensitive programming languages, the ­case is considered different because the ASCII values are not the same. This strncmp function compares strings based on the ASCII values of the characters.
  • All versions of the C language and all compilers (including online compilers) released to date support the strncmp and strcmp functions of the Standard C Library .
  • The strncmp function is much better than the strcmp function . It is because you can use this function to prevent buffer overflows when comparing strings of unknown length. It is because the strncmp function only handles strings up to a certain number of characters for which the comparison result is passed. As an argument when the function is called.
  • The strncmp function works on null-terminated strings , which means the strings to be compared must terminate with the null character '\0' .
  • If the argument 'size' is 0 , the function will always return 0 .
  • If the user wants to compare the two strings without considering the case, the 'strncasecmp' function can be used instead of strncmp . Though this strncasecmp is not a standard C function, it is available on many platforms, including Linux and macOS .
  • The strncmp finds its applications in sorting the strings in alphabetical or lexicographic order.
  • If the user wants to compare the strings containing null characters, use the memcmp function . This memcmp feature takes pointers and a length as arguments and compares the memory at those locations byte-via-byte without regard to null characters.
  • The 'strncmp' feature also can be utilized in combination with other string features, consisting of 'strncpy' or 'strcat' , to manipulate or evaluate substrings of large strings.
  • Additionally, this function may be slow, particularly when comparing long strings or when comparing needs to be performed multiple times. We can use efficient comparison algorithms such as Boyer-Moore or Knuth-Morris-Pratt algorithms to overcome this performance.
  • Suppose we set the 'size' or 'n' to a larger value than the length of both strings. In that case, it results in comparing uninitialized memory, thus can lead to undefined or unexpected behaviour.
  • Also, unexpected results may occur if we work on strings containing non-ASCII characters since this function compares the strings lexicographically, meaning that it considers the order of characters based on their ASCII values .
  • The strncmp characteristic is deterministic in nature, which means that it always returns the identical result for the equal inputs, which allows for trying out and debugging to verify whether given strings are identical or no longer and to pick out the precise position at which those strings vary.
  • Example:

The subsequent code snippet illustrates the utilization of the strncmp function to verify the equality of two provided strings:

Example

#include <stdio.h>
#include <string.h>
int main() {
    char s1[] = "logic practice";
    char s2[] = "logic practice";    
    if (strncmp(s1, s2, strlen(s2)) == 0) {
        printf("The strings '%s' and '%s' are equal.\n", s1, s2);
    }
    else {
        printf("The strings '%s' and '%s' are not equal.\n", s1,s2r);
    }

    return 0;
}

Output:

Output

The strings 'logic practice' and 'logic practice' are equal.

In the provided code snippet, both strings are compared based on the number of characters in string 's2'.

Input Required

This code uses input(). Please provide values below: