# Does the start equal the end?

In this challenge, your task is to write a program or function which takes in a String and outputs a truthy or falsey value based on whether the first character and the last character of the input String are equal.

## Input

You may take input in any way reasonable way. However, assuming that the input is present in a predefined variable is not allowed. Reading from a file, console, command line, input field etc., or taking input as a function argument is allowed.

## Output

You may output in any reasonable format, except for assigning the result to a variable. Writing to a file, console, command line, modal box, function return statements etc. is allowed.

• The input can be empty String as well, for which you should return a falsey value.

• Single-Char Input Strings should have a truthy result.

• Your program should be case-sensitive. helloH should output a falsey value.

• You can only have a single Truthy value and a single Falsey value. For example, outputting false for an Input String and 0 for another input String as Falsey values is not allowed.

• Standard loopholes are not allowed.

## Test Cases

Input    ->    Output

"10h01"        Truthy
"Nothing"      Falsey
"Acccca"       Falsey
"wow!"         Falsey
"wow"          Truthy
"H"            Truthy
""             Falsey


This is , so the shortest code in bytes wins!

• What characters can appear in the input? Printable ASCII? May 13 '17 at 19:35
• @MartinEnder Printable ASCII. Although, I don't think it matters much. Jun 3 '17 at 17:29
• Of course it matters. Some languages can't process non-ASCII characters or null bytes, and in a regex I can match any printable ASCII character with ., but it wouldn't match linefeeds. In general, if you find yourself using the string tag, specify exactly what characters can appear in the input. Jun 3 '17 at 17:31
• @MartinEnder Okay. Will take care in future. Jun 4 '17 at 7:16
• Suggested test case: AbAb => false Oct 31 '17 at 14:40

# Axiom, 30 bytes

f(a)==(#a=0=>false;a.1=a.(#a))


this below seems not to be ok

f(a)==(#a=0=>false;a.#a=a.1)


# TI-Basic (TI-84 Plus CE), 17 bytes

sub(Ans,1,1)=sub(Ans,length(Ans),1


Run with "string":prgmNAME. Returns 1 for true and 0 for false.

# J, 12 bytes

'({.={:)*.*@#


{. means start, = means equals, and {: means end. *.*@# means "logical and with the length of the string", i.e., if the length is 0, it returns 0.

## Powershell, 30 bytes

"$args"|%{!($_[0]-$_[-1]+!$_)}


Try it online

## Clojure: 21, 27 or 34 bytes

depending on the handling of "" test case

true for "aba" and "", false for "abc":

#(=(first %)(last %))


true for "aba", nil for "", false for "abc":

#(first(map =(reverse %)%))


true for "aba", nil for "" and "abc":

#(or(first(map =(reverse %)%))nil)

• This fails for the empty string. May 18 '17 at 0:18
• In comments it was discussed whether it makes sense to define the "correct" answer for an empty string. In Clojure first and last return nil (null) for empty sequences, and nil = nil. May 18 '17 at 12:35
• The discussion ended with keeping the empty string test case as false, because so many answers already had adapted to it. May 18 '17 at 17:09

($a="$args")[0]-ceq$a[-1]  gets the first char and last char, performed a case-sensitive comparison of them. # Perl 5, 22+1 (-p flag)=23 bytes /^(.).*(.)/;$_=$1 eq$2


Outputs 1 for truthy and an empty string for falsey.

# q/kdb+, 2120 18 bytes

Solution:

{#:[x]&(1#x)~-1#x}


Example:

q){#:[x]&(1#x)~-1#x}"abc"
0
q){#:[x]&(1#x)~-1#x}"abca"
1
q){#:[x]&(1#x)~-1#x}""
0


Explanation:

Take the first and last elements of the list, check for equality (return boolean 1 or 0), then check length of string, return the minimum of these two results.

{                } / anonymous lambda function
-1#x  / take (#) 1 item from end of list
(1#x)       / take (#) 1 item from start of list
~      / are they equal
#:[x]             / count (#:) length of list x
&            / minimum


# J, 8 bytes

1 byte thanks to Kritixi Lithos.

1{.{.=|.


Try it online!

• 7 bytes: {:@{.@= Mar 26 '18 at 10:37

# Vim, 30 bytes

:s/\v^(.)(.*\1)?$/1 :s/^..\+/  Leaves you with 1, if the string starts and ends with the same character, or nothing, if it doesn't. # Tcl, 34 bytes proc C s {regexp ^(.)(.*\\1)?$ $s}  Try it online! # Julia 0.6, 29 bytes f(x)=x==""?false:x[1]==x[end]  Try it online! # Perl 6, 18 17 bytes {?m/^(.)[.*$0]?$/}  Test it {?/^(.)[.*$0]?$/}  Test it ## Expanded: { # bare block lambda with implicit parameter ｢$_｣

?        # Boolify the following

/        # match implicitly against ｢$_｣ ^ # beginning of string (.) # character ｢$0｣

[

.*   # followed by any number of characters
$0 # and itself ]? # optionally (Str with a single char)$  # end of string
/
}

• Save a byte: {?/$(/^./&&$/)$/}: matches a character at the start, then if true matches $/ before the end. { ? / $(/^./ &&$/) $/ } Mar 27 '18 at 11:21 • Or even just delete the m... Mar 27 '18 at 11:22 # Husk, 4 bytes Γ·=→  Try it online! Takes a string as an argument to the program, returns 0 for falsy cases and 1 for truthy cases. # Explanation: Γ is list deconstruction. The version I'm using (listN) takes a binary function and returns a function that takes a list. This new function returns the default type (0 in this case) for an empty string, and otherwise applies the given function to the head and tail of the string. The binary function I'm giving it is = (equality), composed on the second argument (·) with → (last element of a list). # SmileBASIC 3, 36 bytes In SmileBASIC, 0 is false and 1 (or nonzero) is true. LINPUT S$L=LEN(S$)?L&&S$[0]==S$[L-1]  Explainer:  LINPUT S$                'read line from console to string S$L=LEN(S$)                'store length of line in L
? L && S$[0] == S$[L-1]
'| |          |
'| |          \-----------first char equals last char
'| \----------------------length is false if zero, && shortcuts
'\------------------------print


# Excel VBA, 52 48 40 38 37 36 34 Bytes

Anonymous VBE immediate window function that takes input of type variant and expected type variant\String from cell [A1] on the ActiveSheet object and outputs boolean response to the VBE immediate window.

?[A1]<>""=([Left(A1)]=[Right(A1)])

• Fails on blank cell. Returns error message instead of falsey. May 16 '17 at 12:43
• @EngineerToast I have corrected for this, though it was more costly than I would have hoped May 16 '17 at 22:20
• I never knew you could use bracketed addresses as a shorthand for the cell value from the active sheet. I learned a thing. Also, I think you can save 4 bytes with Left(a,1)=Right(a,1) instead of the Mids. Right? May 17 '17 at 12:17
• You are absolutely correct @EngineerToast, and on top of that it Left and Right do not return error on empty string input so I can drop the If statement! Thanks :) May 21 '17 at 16:25

# Brachylog, 4 bytes

h.&t


Try it online!

and

# Brachylog, 4 bytes

h~t?


Try it online!

are both predicates which try to unify the first and last elements of the input, outputting through success or failure (which prints true. or false. when it's run as a program).

# GolfScript, 7 bytes

)\(@=\;


Try it online!

## Explanation

)       # The last character
\(     # And the first character
@=\; # Are they equal


# Burlesque, 6 bytes

l_-]==


Try it online!

l_ # Non-destructive tail, leaving the rest of the string on top of the stack
== # Equal


# MAWP, 21 29 bytes

|1A<1:.>1M\%_1A<1:.>\A{0:.}1:


Try it!

Fixed for empty input(and single character input, after Lyxal pointed out). If input is empty, it checks if the existing 1 on the stack is still at the top. then checks the length of the input.

# Golang, 27 bytes

if n[0:1] == n[(len(n))-1:]


Full Program

package main
import (
"fmt"
)
func f(x string) string {
if x[0:1] == x[(len(x))-1:] {
return "Truthy"
} else {
return "Falsey"
}
}
func main() {
var n string
fmt.Scanf("%q", &n)
fmt.Printf("%q", f(n))
}


Try it online!

# Cubix, 20 bytes

@O0UA?n\.\1.1\2ntc?U


Returns 0 for false and 1 for true.

Cubified:

    @ O
0 U
A ? n \ . \ 1 .
1 \ 2 n t c ? U
. .
. .


explanation:

A - read in input as chars; so input of abc has the stack -1,99,98,97
? - turn left if negative (empty string), right if positive
if negative:
0 - push 0
U - left u-turn
O - output as int
@ - end program
if positive:
\ - reflect so we are moving to the right
2 - push 2
n - negate top of stack
t - pop top of stack, move that item from the bottom to the top; moves the last character to top of stack
c - xor
? - turn right if positive, go straight if negative
if 0:
U - left U-turn
1 - push 1
\ - reflect so we point up
O - output 1
@ - end program
if positive
n - negate
\ - reflect
n - negate
? - righthand turn
0 - push 0
U - lefthand U-turn
O - output
@ - end program


Try it online!

Watch it online

# C (gcc), 45 bytes

f(s,j)char*s;{for(j=*s;*s;s++);j=j&&j==*--s;}


Try it online!

# SmileBASIC, 29 bytes

DEF E S?S>""&&POP(S)==S[0]END


Pretty simple, but it's important that the POP is done after the first character is checked, so it will work on 1 character long strings.

# ActionScript 2.0, 49 57bytes

function a(b){trace(b!=""?b.substr(0,1)==b.slice(-1):0);}


Old version (v) doesn't output false for "", but the new, longer version does.

function a(b){trace(b.substr(0,1)==b.slice(-1));}


I'm fairly sure this is the shortest way to do it, but...

The semicolon can be removed and it'll still compile in JPEXS.

This is a pretty simple function - if the argument is not "", output whether the (0, 1) substring of the argument (basically a way of getting the first character) is equal to the last character, obtained by slicing the string at its last character, otherwise 0. Tracing is shorter than returning.

• What does this do for the empty string? Mar 27 '18 at 1:57
• Fixed. Now it checks for the empty string and returns 0. Mar 27 '18 at 7:54

# ><>, 11 bytes

l?!<{:}=n;!


Try it online!

Takes input through the -s flag. You'll need to remove the flag for an empty input.

### How it works

l?!<  Go right if there is input, left is there is none
{:  If there is input, rotate the stack and dupe the first element
This creates two copies of a one character input
}=n;  Print if the last character is equal to the first
l  If there is no input, push the length of the stack (0)
n;!  Wrap around and print the 0


# jq, 23 characters

(20 characters code + 3 characters command line option)

.>""and.[:1]==.[-1:]


Sample run:

bash-4.4$jq -R '.>""and.[:1]==.[-1:]' <<<$'10h01\nNothing\nAcccca\nwow!\nwow\nH\n'
true
false
false
false
true
true
false


Try it online!

### jq, 25 characters

(22 characters code + 3 characters command line option)

endswith(.[:1])and.>""


Just to show that jq also has endswith() function. But unfortunately all strings are considered to end with empty string, so is not shorter. ☹

# Tcl, 59 bytes

proc C s {expr {[string in $s 0]==[string in$s e]&\$s!=""}}


Try it online!

# MAWP 2.0, 28 bytes

=A|_!=W1-W0-*{=M~M-{0=A}}A:
`

Try it!