Difference between revisions of "Manual:DIL Manual/strcmp()"

From DikuMUD Wiki
Jump to navigation Jump to search
(XML import)
(checked against the compiler and the interpreter; example compiles)
Line 1: Line 1:
−
<span id="bfstrcmp"></span>
+
= strcmp() =
 +
'''Function''' (integer). Compares two strings, minding upper and lower case.
  
−
  '''Function:'''  <i><!--CODE-->integer strcmp( s1:string, s2:string) ;</i><!--ENDCODE-->
+
  strcmp(s1 : string, s2 : string)
  
−
<!--TERM--> '''s1'''
+
DIL's <code>==</code> and <code>!=</code> on strings ignore case (<code>"Sofa" == "sofa"</code> is
−
<!--DEFINITION-->         first string
+
true). Use strcmp() when case matters, as with passwords: it compares the two strings byte by
−
<!--TERM--> '''s2'''
+
byte, as the C function of that name does.
−
<!--DEFINITION-->         second string
 
  
−
  '''returns'''
+
== Parameters ==
−
<!--TERM-->  '''-1'''
+
{| class="wikitable"
−
<!--DEFINITION-->        if s1 &lt; s2
+
! Name !! Type !! Meaning
−
<!--TERM-->  '''0'''
+
|-
−
<!--DEFINITION-->        if s1 = s2
+
| s1 || string || The first string.
−
<!--TERM-->  '''1'''
+
|-
−
<!--DEFINITION-->        if s1 &gt; s2
+
| s2 || string || The second string.
 +
|}
  
 +
== Returns ==
 +
Integer: 0 when the strings are the same, less than 0 when <code>s1</code> sorts before
 +
<code>s2</code>, more than 0 when after. Test the sign only: the value need not be -1 or 1.
  
−
This allows you to compare strings with case sensitivity in place.  If
+
A string variable that was never set counts as <code>""</code>. It fails when an argument is a
−
you don't care about the case of the string use the normal '==' '&gt;',
+
failed value.
−
'&lt;', '&lt;=', '&gt;=', symbols.
 
−
'''example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
  
−
if (strcmp("I Care about Capitals",s2)==0))
+
== Notes ==
−
---~---~---~---~---~---~---~---~---
+
* Capitals sort before small letters: <code>strcmp("Z", "a")</code> is less than 0.
 +
* The old manual said <code>&lt;</code> and <code>&gt;</code> ignore case; only <code>==</code>
 +
  and <code>!=</code> do.
  
−
</i><!--ENDCODE-->
+
== Example ==
−
<span id="bfstrcmp"></span>
+
<pre>
 +
dilbegin integer same_pwd(first : string, second : string);
 +
code
 +
{
 +
  return (strcmp(first, second) == 0);
 +
}
 +
dilend
 +
</pre>
 +
Tells whether a password was typed the same way twice, capitals included, as the password
 +
change in commands.zon checks with <code>excmdstr_case</code>.
  
−
'''Function:'''  <i><!--CODE-->integer strcmp( s1:string, s2:string) ;</i><!--ENDCODE-->
+
== See also ==
−
 
+
* [[Manual:DIL Manual/strncmp()|strncmp()]]
−
<!--TERM-->  '''s1'''
+
* [[Manual:DIL Manual/excmdstr case|excmdstr_case]]
−
<!--DEFINITION-->        first string
+
* [[Manual:DIL Manual/tolower()|tolower()]]
−
<!--TERM-->  '''s2'''
+
* [[Manual:DIL Manual/Operators|Operators]]
−
<!--DEFINITION-->        second string
 
−
 
 
−
  '''returns'''
 
−
<!--TERM-->  '''-1'''
 
−
<!--DEFINITION-->        if s1 &lt; s2
 
−
<!--TERM-->  '''0'''
 
−
<!--DEFINITION-->        if s1 = s2
 
−
<!--TERM-->  '''1'''
 
−
<!--DEFINITION-->        if s1 &gt; s2
 
−
 
 
−
 
 
−
This allows you to compare strings with case sensitivity in place.  If
 
−
you don't care about the case of the string use the normal '==' '&gt;',
 
−
'&lt;', '&lt;=', '&gt;=', symbols.
 
−
'''example:'''
 
−
<i><!--CODE-->
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
if (strcmp("I Care about Capitals",s2)==0))
 
−
---~---~---~---~---~---~---~---~---
 
−
 
 
−
</i><!--ENDCODE-->
 

Revision as of 23:20, 14 November 2026

strcmp()

Function (integer). Compares two strings, minding upper and lower case.

strcmp(s1 : string, s2 : string)

DIL's == and != on strings ignore case ("Sofa" == "sofa" is true). Use strcmp() when case matters, as with passwords: it compares the two strings byte by byte, as the C function of that name does.

Parameters

Name Type Meaning
s1 string The first string.
s2 string The second string.

Returns

Integer: 0 when the strings are the same, less than 0 when s1 sorts before s2, more than 0 when after. Test the sign only: the value need not be -1 or 1.

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

Notes

  • Capitals sort before small letters: strcmp("Z", "a") is less than 0.
  • The old manual said < and > ignore case; only ==
 and != do.

Example

dilbegin integer same_pwd(first : string, second : string);
code
{
   return (strcmp(first, second) == 0);
}
dilend

Tells whether a password was typed the same way twice, capitals included, as the password change in commands.zon checks with excmdstr_case.

See also