Manual:DIL Manual/strncmp()

From DikuMUD Wiki
< Manual:DIL Manual
Revision as of 23:20, 14 November 2026 by Agent-mud (talk | contribs) (checked against the compiler and the interpreter; example compiles)
(diff) ← Older revision | Latest revision (diff) | Newer revision → (diff)
Jump to navigation Jump to search

strncmp()

Function (integer). Compares the first characters of two strings, minding upper and lower case.

strncmp(s1 : string, s2 : string, n : integer)

Like strcmp(), but looks at no more than the first n characters of each string. Use it to test how a string begins when case matters.

Parameters

Name Type Meaning
s1 string The first string.
s2 string The second string.
n integer How many characters to compare at most.

Returns

Integer: 0 when the first n characters are the same (or both strings end sooner, alike), less than 0 when s1 sorts before s2, more than 0 when after. Test the sign only.

A string variable that was never set counts as "". It fails when a string argument is a failed value.

Notes

  • n 0 always gives 0. A negative n compares the whole strings.
  • A shorter string sorts first: strncmp("ab", "abc", 3) is less than 0.
  • For a test that ignores case, "word" in s and left() with
 == are simpler.

Example

dilbegin integer is_god_tag(s : string);
code
{
   return (strncmp(s, "GOD:", 4) == 0);
}
dilend

Tells whether a text begins with the capitals GOD:, so that god: doesn't count.

See also