Skip to content

strncmp

Comparing the first n elements of two character arrays in a case-insensitive manner

strcmp(s1, s2, n)

  • s1 and s2 should be arrays of characters.
  • n should be a positive integer.
  • It returns logical 1 if the first n elements of s1 and s2 have the same elements, and returns logical 0 if otherwise.
  • s1 and s2 are compared case-insensitively.
  • Elements of s1 and s2 are indexed linearly. See Example 1 below.
  • It returns logical 0 if any of s1 and s2 is not a character array.

Example 1: The following inputs a and b have different sizes. But, strncmp compares their first n characters using linear indexing.

a=['ace';'bdf']
b=['abcdex']
strncmp(a,b,5)
strncmp(a,b,6)
a =
ace
bdf

b =
abcdex

ans =
1

ans =
0