strncmp - C string.h

C examples for string.h:strncmp

Type

function

From

<cstring> 
<string.h>

Description

Compares up to num characters of the C string str1 to those of the C string str2.

Prototype

int strncmp ( const char * str1, const char * str2, size_t num );

Parameters

Parameter Description
str1 C string to be compared.
str2 C string to be compared.
num Maximum number of characters to compare.

Return Value

Returns an integral value indicating the relationship between the strings:

return value indicates
<0 the first character has a lower value in str1 than in str2
0 the contents of both strings are equal
>0 the first character has a greater value in str1 than in str2

Demo Code


#include <stdio.h>
#include <string.h>

int main()/*w w  w  .j  a v a 2 s  .c om*/
{
  char str[][10] = { "test" , " test" , "test " };
  int n;
  for (n = 0; n<3; n++) {
    if (strncmp(str[n], "texx", 2) == 0)
    {
      printf("found %s\n", str[n]);
    }
  }
  return 0;
}

Related Tutorials