AppDividend
Latest Code Tutorials

C++ strncmp() Function Example | C++ Standard Library

0

C++ strncmp() is an inbuilt function that is used for string handling. It is defined in string.h header file. The strncmp() function is a predefined library which is used for comparing two strings of which the characters are terminated by null characters. The comparison is done lexicographically and returns an integer.

C++ strncmp() Function

The std::strncmp() function lexicographically compares not more than count characters from a two null-terminated strings and returns the integer based on the outcome.

  1. The strncmp() function takes two strings and a number num as arguments and compares at most first num bytes of both the strings.
  2. The num should be at most equal to a length of the longest string. If the num is defined greater than a string length than comparison is done till the null-character(‘\0’) of either string.
  3. The strncmp() function compares the two strings lexicographically. It starts the comparison from a first character of each string. If they are equal to each other, it continues and compares the next character of each string and so on.
  4. This process of comparison stops until the terminating null-character of either string is reached or num characters of both the strings matches.

Syntax

int strncmp(const char* lhs_var, const char* rhs_var, size_t x)

Here the function takes three parameters and returns an integer in the main function.

Parameters

It takes 3 parameters lhs_var, rhs_var, and the count. It basically compares the characters of lhs_var and rhs_var lexicographically up to the count of the variable “x”.

The behavior of the function is undefined if the string is not ending with null characters.

Return Value

It returns an integer less than 0 if the first string is less than the second string, equal to zero if the first string is equal to the second string and greater than 0 if the first string is greater than the second string.

Difference between strncmp() and strcmp()

The main difference between strncmp() and strcmp() is that in case of strncmp() comparison is done up to x characters in the string while in the case of strcmp() it is done till the strings are terminated by the null character.

Example programs on strncmp() function in C++

Example 1: Write a program to show the working of the strncmp() function.

See the following code.

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char str_var1[20] = "Hello World!";
  char str_var2[20] = "Hello AI";
  int k;
  k = strncmp(str_var1, str_var2, 8);
  if (k == 0)
  {
    cout << "String are equal upto specified characters.";
  }
  else
  {
    cout << "Strings are not equal upto the specified characters.";
  }
}
Output
Strings are not equal up to the specified characters.
Example 2: Write a program to compare two equal strings using strncmp() function.

See the following code.

#include <iostream>
#include <string.h>

using namespace std;

int main()
{
  char str_var1[20] = "Hello World!";
  char str_var2[20] = "Hello AI";
  int k;
  k = strncmp(str_var1, str_var2, 5);
  if (k == 0)
  {
    cout << "String are equal upto specified characters.";
  }
  else
  {
    cout << "Strings are not equal upto the specified characters.";
  }
}
Output
Strings are equal up to specified characters.

See also

C++ strcmp()

C++ string

Leave A Reply

Your email address will not be published.

This site uses Akismet to reduce spam. Learn how your comment data is processed.