# Determine if Strings are equal

Your task is simple. Determine if one string equals the other (not address, the value) without the use of equality operators (such as ==, ===, or .equal()) or inequality (!=, !==) anything similar for other languages. This means anywhere! You may not use these operators anywhere in the code. You may however, you use toggles such as !exp as you're not directly comparing the exp != with something else.

In addition, you may not use any functions such as strcmp, strcasecmp, etc.

As for comparison operators (>=, <=, >, <), they are also disallowed. I realize that some answers include this, but I'd really like to see more answers that don't borderline the equality operator.

An example using PHP is shown:

<?php

$a = 'string';$b = 'string';

$tmp = array_unique(array($a, $b)); return -count($tmp) + 2;


Simply return true or false (or something that evaluates in the language to true or false like 0 or 1) to indicate if the strings match. The strings should be hardcoded seen in the above example. The strings should not be counted in the golf, so if you declare the variable before hand, don't count the declaration.

• Is it necessary to output the result, or simply write a function to return a bool? If writing a complete program is required, that could make-or-break answers in languages with (relatively) significant boilerplate to create a functioning executable like Java and C# (such is the nature of the beast, but this challenge has little in the way of concrete guidelines, leaving much to interpretation/choice). And how are we to take the strings? Hardcoding, reading from STDIN, pass as command-line arguments? – Tony Ellis Mar 12 '14 at 14:23
• Is this [code-golf] or a [popularity-contest]? It can't be both. – Gareth Mar 12 '14 at 14:23
• Sorry, I have modified my questions to reflect both comments. – David Chen Mar 12 '14 at 14:32
• So inequality is allowed? – user80551 Mar 12 '14 at 14:41
• If the strings are to be hardcoded more than once(each) , do i have to count their length? – user80551 Mar 12 '14 at 15:13

## C, 48 chars

c(char*a,char*b){return*a++-*b?0:!*b++||c(a,b);}


Recursion is often shorter than a loop.
Subtraction easily replaces a comparison.

Lua, 147

a="string" b="string" y=string.byte s=string.sub m=math.abs c=1 for i=1,math.max(#a,#b) do d=y(s(a,i)) e=y(s(b,i))if not((d and e)and(m(d) - m(e)))then c=0 break end end


Assumes a and b are the strings. c should be 1 (true)

## Python 56

a = "string"
b = "string"

not any(ord(x)*len(a)-ord(y)*len(b) for x,y in zip(a,b))


A bit longer than the shortest Python examples, but probably about the best I can do without using in or set notation (the former should probably be excluded, the latter has already been employed as succinctly as possible).

# Rebol, 22

all[find a b find b a]


This will return none if strings are different.

Example usage in Rebol console:

>> a: "string"
== "string"

>> b: "string"
== "string"

>> c: "stringstring"
== "stringstring"

>> all [find a b find b a]
== "string"

>> all [find a c find c a]
== none


By default Rebol is case insensitive. So use find/case to enforce stricter test:

>> b: "STRING"
== "STRING"

>> all [find a b find b a]
== "STRING"

>> all [find/case a b find/case b a]
== none


# Javascript

Where x = z and x != y:

if( x.match(RegExp(z)) ) // Evaluates to True

if( x.match(RegExp(y)) ) // Evaluates to False

• What if z === 'Abc.efg' and x === 'Abcdefg'? – Not that Charles Mar 27 '14 at 22:10

Bash, 14

Assuming strings are in A and B, then

[ -z ${A/$B} ]


will return a true (0) or false (1) value in $?. How do I use it? A="string";B="string";[ -z${A/$B} ]&&echo same  How does it work?${A/$B} replaces$B in $A with nothing. Only if$A=$B then result will be an empty string. [ -zn X ] tests for empty or null string and sets$? to 0 if empty.

Update: should be -z not -n in test.

• It doesn't work here: pastebin.com/mv9RLC9p – manatwork Mar 17 '14 at 9:41
• @manatwork, thanks. should be -z. – philcolbourn Mar 20 '14 at 5:56

## Windows Batch (47)

@echo.>%1
@if exist %2 (exit /b 0)
@exit /b 1


This should be run in an empty directory each time ;)

It types a blank line into a file named with the first argument, and then checks if the second argument exists. For hardcoded strings, either replace the %1 and %2 or add @if "%1"=="" %0 string1 string2 to the top of the file.

• Do you have a reference for your “errorlevel isn't 0 by default” comment? – manatwork Mar 20 '14 at 10:55

# Java

import java.util.HashSet;
public class Equals {
public static boolean equals(String s1, String s2){
HashSet<String> sSet = new HashSet<>();
}
public static void main(String[] args) {
System.out.println(equals("Apple","Tpple"));
}
}

• This can still be golfed a bit. Also you should include the bytecount in the title of your answer :] – Poke Mar 26 '17 at 2:04

# perl, 20 bytes

Today I'm serving up perl two ways, saving a byte or two off the other perl solutions seen here. First, with hash slices, for 20 bytes:

@x{$a,$b}=();$_=%x%2  and then a port of the clever python solution above (also 20 bytes) $_={$a,0,$b,1}->{$a}  # C - 59 e(char*l,char*r){for(;*l&*r&&!(*l++^*r++););return!*l&!*r;}  # Ruby (18) This one is a script using the exit status instead of a function's return value: 0 is success, 1+ is failure. exit$*.uniq.size-1


Usage:

$ruby equality.rb a b$ echo $? 1$ ruby equality.rb a a
$echo$?
0


# R, 4

"string1"%in%"string2" equivalent to is.element("string1", c("string2"))

## D - 34 27 bytes

!any!"a.length"(split(a,b))


Example use:

import std.stdio,std.string,std.algorithm;

void main() {
auto a = "abcd";
auto b = "abcd";

writeln(!any!"a.length"(split(a,b)));
}


Output:

true


# ~-~! (No Comment) - 1

### (might not be legal)

|string|-|string|


% if they're equal, anything else if they're not. There's an issue though; ~-~! only accepts % or ~ (0 or 1) for conditionals; if it's neither, it throws an error. So you'd have to use ==~ to actually be able to use it properly. I don't think there /is/ a way to do this in ~-~! without the equality operator if this isn't allowed.

# Excel VBA, 124 Bytes

Subroutine that takes variant input a and b of the assumed type variant\string and outputs as a boolean variable to the VBE immediates window

Sub m(a,b)
c=1
For i=1To Len(a)
If Asc(Right(Left(a,i),1))-Asc(Right(Left(b,i),1)) Or Len(a)-Len(b) Then:c=0:Exit For
Next
Debug.?CBool(c)
End Sub


# Operation Flashpoint scripting language, 7 bytes

a in[b]


Strings are stored in variables a and b (as allowed by the question).

Interestingly, even if direct comparison would be allowed, this would be the only valid solution, because a==b performs a case-insensitive comparison, whereas the built-in in uses case-sensitive comparison for strings internally.

# Java - 54

First Golf: Since all string's HashCode is the same with the same content, it can be useful.

(int)Math.abs(Math.signum(s.hashCode()-b.hashCode()));

• This is wrong. Non-identical strings may have different hash codes. – Konrad Borowski Mar 13 '14 at 13:37

## C#

var b="string";
var a="string";
return  Regex.IsMatch(b, "^.{" + a.Length + "}$") && Regex.IsMatch(b,"^" + a + "$");

• I am afraid this counts as one of the forbidden functions. – Hosch250 Mar 12 '14 at 15:53
• awh.. I hope now it is fine return Regex.IsMatch("^" + a + "\$", b); is it ? – sm.abdullah Mar 13 '14 at 13:38
• According to your function, a = "string.!?" equals `b = "stringz". – CompuChip Mar 13 '14 at 14:33
• again...... : P – sm.abdullah Mar 13 '14 at 16:27