[Question]
  [
**Closed**. This question needs [details or clarity](/help/closed-questions). It is not currently accepting answers.
 
 
---
**Want to improve this question?** Add details and clarify the problem by [editing this post](/posts/23860/edit).
Closed 4 years ago.
[Improve this question](/posts/23860/edit) 
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](https://www.php.net/strcmp), [strcasecmp](https://www.php.net/manual/en/function.strcasecmp.php), 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.
  
[Answer]
# Python 49 45 18 22 15 14
## ( + 3 if string variables are considered)
```
print{a:0,b:1}[a]
```
The string should be hard coded at the two occurrences of `a` and one occurrence of `b` surrounded by quotes.
`a` and `b` should be pre-initialized to the strings.
---
# Python shell, 9
## ( + 3 if string variables are considered)
```
{a:0,b:1}[a]
```
Output in shell
```
>>> a = 'string'
>>> b = 'string'
>>> {a:0,b:1}[a]
1
>>> a = 'string'
>>> b = 'stringgg'
>>> {a:0,b:1}[a]
0
>>> {'string':0,'string':1}['string']
1
>>> {'stringggg':0,'string':1}['stringggg']
0
>>> 
```
---
Explanation
Creates a dict(hash table) with the key of first and second string.
If second string is the same, the value of first is replaced by that of second.
Finally, we print the value of first key.
EDIT: OP allowed 0/1 instead of False/True as well as using pre-initialized variables.
[Answer]
# Python (~~17~~ 11):
```
b in a in b
```
(Checks if b is contained in a and a is contained in b, if that wasn't clear from the code.)
## Alternative python: (~~8~~ 7)
derived from Tom Verelst's Go solution:
```
b in[a]
```
Bonus: this works for any type.
EDIT:
Wait a second, just read that you can also directly program in the strings, and don't have to count quotes... (or at least, that what golfscript does). So... Python on par with golfscript? Oh my!
## Alternative alternative Python (~~5~~ 4):
(thanks Claudiu)
```
"string"in["string"]
```
original:
```
"string" in["string"]
```
## Alternative Alternative Alternative Bendy-ruly Python (2):
```
"string"is"string"
```
Nothing was said about comparison **keywords** (This is not a serious submission, just something that occurred to me...)
[Answer]
# JavaScript, 11 10
Strings have to be stored in a and b.
```
!(a>b|a<b)
```
Edit: thanks Danny for pointing out, `|` is enough instead of `||`
[Answer]
# Ruby, 11
```
s = 'string'
t = 'string'
!!s[t]&t[s]
```
Checks if each string is contained within the other.
[Answer]
# Python - 11 (without the strings)
```
>>> a = 'ss'
>>> b = 's'
>>> a in b in a
False
```
[Answer]
## GolfScript (5 chars)
```
'string1''string1'].&,(
```
Fairly straightforward port of the PHP reference implementation. Leaves `0` (=false) on the stack if the strings are the same, or `1` (=true) if they're different.
[Answer]
# Javascript (45 bytes):
Here is another solution in Javascript.
```
var a='string',b='string',c=!a.replace(b,'');
```
The space is important.
`c` should be `true`.
[Answer]
# C++, ~~63~~ ~~58~~ 56
```
const char* a = "string";
const char* b = "string";
int main(){while(*a**b*!(*a^*b))++a,++b;return!(*a^*b);}
```
[Answer]
# coreutils: uniq -d
Just enter your two strings as the standard input of a pipe and `uniq -d | grep -q .` will print nothing but will have a return value of success or error. If you want to print the boolean, just replace with `uniq -d | grep -c .`
How many characters? I let you count; `uniq -d|grep -q .` with no extra spaces has 17 characters in it, but since the whole job is performed by uniq, I would say this solution is a 0-character one in... `uniq` own language!
Actually, `uniq -d` will print one line if the two strings are identical, and nothing if the are different.
[Answer]
Strings have to be stored in a and b. Will not work if either is `null`.
# C#, 53
```
string.IsNullOrEmpty(a.Replace(b,"")+b.Replace(a,""))
```
---
# C#, 28
```
a.Contains(b)&&b.Contains(a)
```
[Answer]
### PHP - 49 characters
```
!(strlen($a)^strlen($b)|strlen(trim($a^$b,"\0")))
```
[Answer]
## APL (~~8~~ 9)
Update: the old one does not work for strings of different lengths.
```
{∧/∊⌿↑⍺⍵}
```
* `↑⍺⍵`: make a matrix with `⍺` on the first line and `⍵` on the second line, filling blanks with spaces.
* `∊⌿`: For each column, see if the upper row contains the lower row (as in the old version).
* `∧/`: Take the logical `and` of all the values.
Old one:
```
{∧/⍺∊¨⍵}
```
* `⍺∊¨⍵`: for each combination of elements in `⍺` and `⍵`, see if the element from `⍺` contains the element from `⍵`. Since in a string these will all be single characters, and a string contains itself, this is basically comparing each pair of characters.
* `∧/`: take the logical and of all the values (if all the characters match, the strings are equal)
[Answer]
# Python - 12
```
not({a}-{b})
```
This solution uses sets. Subtracting equal sets will result in an empty set, which has a boolean value of False. Negating that will result in a True value for a and b being equal strings.
```
>>> a="string1"
>>> b="string2"
>>> not({a}-{b})
False
>>> a="string"
>>> b="string"
>>> not({a}-{b})
True
```
Edit: Thanks to Peter Taylor for pointing out the unnecessary whitespace.
[Answer]
## C — 62
```
e(char*p,char*q){for(;*p&*q&&!(*p^*q);q++,p++);return!(*p^*q);}
```
Tested. Call as `e(str1, str2)`
Come to think of it, if you don't count the `char*p,char*q`, which seems only fair, it's only 49 bytes :)
[Answer]
# Haskell -- 9
```
elem a[b]
```
Note that this, just like many entries here, is just an expression. This is not a Haskell program.
[Answer]
## Java - 162 147 characters
The idea is to compare the difference of each byte, same bytes will have difference 0.
The program will throw `java.lang.ArrayIndexOutOfBoundsException` for when bytes are different (try to access a negative index) or when strings are of different length. It will catch the exception and return 0 (strings not equal), or return 1 otherwise (strings equal).
**Compressed:**
```
String a = "12345";
String b = "12345";
byte[]x=a.getBytes(),y=b.getBytes();int z,i=a.length()-b.length();try{for(byte d:x){z=d-y[i];z=x[-z*z];i++;}}catch(Exception e){return 0;}return 1;
```
**Normal:**
```
String a = "12345";
String b = "12345";
byte[] byteArrA = a.getBytes();
byte[] byteArrB = b.getBytes();
int byteDifference = 0;
int i = a.length() - b.length();
try {
    for (byte aByte : byteArrA) {
        byteDifference = aByte - byteArrB[i];
        byteDifference = byteArrA[-byteDifference*byteDifference];
        i++;
    }
} catch (Exception e){
    return 0;
}
return 1;
```
[Answer]
# PHP
```
    $string = 'string';
    isset( ${'string'} );
```
This script may not have any utility, but atleast this provides a way to compare strings.
# PHP
Aother one:
```
    $string = 'something';
    $text   = 'something';
    return count( array( $string => 1 , $text => 1 ) ) % 2;
```
[Answer]
# Prolog 7
```
e(A,A).
```
This makes use of the pattern matching feature in Prolog to unify the 2 arguments to the predicate, which effectively tests for equals equivalence *when there is no unbound variable*. 
Sample usage:
```
?- e("abcd", "Abcd").
false.
?- e("abcd", "abcd").
true.
```
Technically speaking, the behavior of this solution is that of unification operator [`=/2`](http://www.swi-prolog.org/pldoc/doc_for?object=%28%3D%29/2), rather than that of [`==/2`](http://www.swi-prolog.org/pldoc/man?section=standardorder), which checks for term equivalence. The difference shows when unbound variables are involved. In this solution, when unbound variable is supplied, the predicate will return `true` when unification is successful. In comparison, `==/2` will compare order of term without unification.
[Answer]
## PHP, 21
This one is doing the job using variable indirection.
```
$$a=$b;!!$$b;
```
Or, if you don't need it to be bool
```
$$a=$b;$$b;
```
**EDIT** : I forgot to handle the case where you try to compare two empty strings, so the code now is
```
$$a=$b;!($a.$b)||$$b;
```
which is 21 chars.
[Answer]
## CPython: 6
```
a is b
```
---
```
>>> a = 'string'
>>> b = 'string'
>>> c = 'STRING'
>>> a is b
True
>>> a is c
False
```
The use of `is` is obviously pretty suspect, but since the task specifically calls out that we're to determine value equality rather than reference equality, and `is` only compares object identity, I feel like it *may* not fall under the list of banned operators.
Of course there's also some question as to whether this is even valid; it works on all of my systems, but it's implementation-specific and probably won't always work if the strings aren't defined by hand in the interactive interpreter.
[Answer]
## Mathematica / Wolfram Language, 15 bytes
```
2 - Length[{a} ∪ {b}]
```
Pretty self explanatory, sets each string as a set, then checks the length of the union of the two sets. If the strings are the same, returns 1, otherwise returns 0. If I'm allowed to return '2' for "different" and '1' for "same", subtract two bytes.
[Answer]
**C 342 golfed**
```
#include <stdio.h>
#define N 100
#define P(x) printf("%s\n",x)
#define G(x) gets(x)
void e(int x){x?P("y"):P("n");}
int main(){
char s[N],t[N];char *p,*q;int r=0; int n=0,m=0;int i=1;p=s,q=t;
if((p=G(s))&&(q=G(t))){while (*p){n+=i*(int)*p;m+=i*(int)*q;i++;p++;q++;if(!(*q)){break;}}
if(!*p&!*q){if(!(m-n)){r=1;}}e(r);}
return 0;
}
```
Note: Visual Studio complains if you don't use their safe methods eg gets\_s.
CodeBlocks with mingw compiles without warnings.
**C 655 not golfed**
Code creates weighted sum of chars for each string.
If the difference is zero then they are equal, including 2 empty strings:
```
    #include <stdio.h>
#define N 100
#define P(x) printf(x)
#define G(x) gets_s(x)
void e(int x){ x ? P("equal\n") : P("not equal\n"); }
int main()
{
    char s[N], t[N];//words
    char *p = 0, *q = 0;
    int r = 0; //result 0=false
    int n=0, m=0; //weighted sums
    int i = 1; //char pos start at 1
    if ((p=gets_s(s)) &&
        (q=gets_s(t)))
    {
        while (*p)
        {
            n += i*(int)*p;
            m += i*(int)*q;
            i++;
            p++;
            q++;
            if (!(*q)){
                break;
            }
        }
        if (!*p && !*q){ //both same length strings
            if (!(m - n)){ r = 1; } //weighted sums are equal           
        }//else r=0, false=>not equal
        e(r);
    }
    else{
        P("error\n");
    }
    getchar();
}
```
[Answer]
# Python
It's long and it's not beautiful, but this is my first entry!
```
def is_equal(a,b):
    i=0
    a,b=list(a),list(b)
    if len(a)>len(b):
        c=a
        lst=b
    else:
        c=b
        lst=a
    try:
        while i<len(c):
            for j in c:
                if j not in lst[i]:
                    return False
                i+=1
    except IndexError:
        return False
    return True
```
[Answer]
# R (4 chars)
```
'string1'%in%'string2'
```
Or, for an actual function,
## (6 chars)
```
`%in%`
```
which is called with
```
`%in%`(x,y)
```
As R is a vector-based language, is treated as a string and b is treated as a vector of one string, so this is similar to the `elem a[b]`/`a in b in a`/`a in[b]` solutions in spirit.
[Answer]
## PHP, 68 Bytes
I assume you're prohibited to use **any** comparison operators. So `<` or `>` are included.
The idea is to use bitwise XOR. In different languages this operator has different syntax - I'll show an example for PHP. There it's available with `^`. Unfortunately, its behavior with strings isn't as good as it could be, so you'll need to check string length before. That is because in PHP, xor will strip the longer string down to the length of the shorter string. 
Next thing is to work with strings properly, because a single `xor` will not produce a result, available for further operations in PHP. That's why [`unpack()`](http://php.net/unpack) was used. So, the code would be:
```
return !(strlen($a)^strlen($b)) & !array_filter(unpack('c*', $a^$b))
```
It's longer than option with `<` / `>` but it won't use them. Also, the important thing is about [PHP type juggling](http://www.php.net/manual/en/language.types.type-juggling.php) (so empty array will be cast to `false`). Or maybe there's a simpler way to check if an array contain non-zero members (*Edit*: while I've been typing this, there's a good catch with `trim()` in another answer, so we can get rid of array operations)
But I believe there are languages, where we can do just `a ^ b` - literally, getting the result. If it's `0` (treated from all resulted bytes) - then our strings *are equal*. It's very easy and even more simple than `<` or `>` stuff.
[Answer]
# grep 14 characters
Of course, I only count the grep code; the two strings are on two consecutive lines in the input (either a pipe or a file or even an interactive session).
```
$ echo -e 'string\nstring' | grep -cPzo "(?s)^(\N*).\1$"
1
$ echo -e 'string\nstring1' | grep -cPzo "(?s)^(\N*).\1$"
0
$ echo -e 'string1\nstring' | grep -cPzo "(?s)^(\N*).\1$"
0
```
[Answer]
# Matlab: 12 chars (after the strings are in variables)
```
~(x*x'-y*y')
```
The code including assignments would be:
```
x='string1'
y='string2'
~(x*x'-y*y')
```
[Answer]
# The very crazy way
Just for the fun, but many ways for making it fail if one thinks about it. More over, don't forget the strings will be EXECUTED by the shell.
```
$ echo -e 'string\nstring1' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true
$ echo -e 'string\nstring' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true
true
$ echo -e 'string1\nstring' | sed -e '1s/^/#define /' | cpp | sh 2>/dev/null && echo true
```
A good counter-example is comparing "string" as first string and "rm -Rf /" as a second string; just check as root and see: it will say "true" though both strings obviously aren't the same.
[Answer]
# JavaScript [18 bytes]
```
(_={})[a]=1,!!_[b]
```
**OR**
```
!!((_={})[a]=_)[b]
```
This will return `true` if `a == b` and `false` if `a =/= b`. The logic behind is creating an object with a value of `a` as a property and returning `1` or `undefined` in case if a property of `b` value exists or doesn't exist in that object.
[Answer]
# JavaScript [15 bytes]
```
![a].indexOf(b)
```
This will return `true` if `a == b` and `false` if `a =/= b`. The script is looking for the value of `b` in the array that holds a single element of value of `a`.
]