# Does the nth char equal the nth from last char?

Inspired by Does the start equal the end

Given a string s and an integer n, output a truthy/falsey as to whether the nth char in s equals the nth from the end char in s.

# Input

A non-empty string and an integer. You can use 0-based indexing or 1-based indexing. The integer is guaranteed to be valid based on the string. For example, if the string is "supercalifragalistic123", the integer can be from 1 to 23 for 1-based indexing, and 0 to 22 for 0-based indexing. Please note that n can be larger than half the length of s.

Input is limited to printable ASCII.

# Output

A truthy/falsey value based on whether the nth value in s equals the nth from last value in s.

Please note that the last char is in position 0 for 0-based indexing and position 1 for 1-based indexing. Think of it as comparing the string to its reverse.

# Test Cases

0-indexed

"1", 0         Truthy 1 == 1
"abc", 1       Truthy b == b
"aaaaaaa", 3   Truthy a == a
"[][]", 1      Falsey ] != [
"[][]", 0      Falsey [ != ]
"ppqqpq", 2    Truthy q == q
"ababab", 5    Falsey a != b
"12345", 0     Falsey 1 != 5
"letter", 1    Truthy e == e
"zxywv", 3     Falsey w != x


1-indexed

"1", 1         Truthy 1 == 1
"abc", 2       Truthy b == b
"aaaaaaa", 4   Truthy a == a
"[][]", 2      Falsey ] != [
"[][]", 1      Falsey [ != ]
"ppqqpq", 3    Truthy q == q
"ababab", 6    Falsey a != b
"12345", 1     Falsey 1 != 5
"letter", 2    Truthy e == e
"zxywv", 4     Falsey w != x

• Sandbox (deleted) May 31, 2017 at 20:50
• Would it be acceptable to take n as a code-point? (for esoteric languages such as brain-flak) May 31, 2017 at 21:29
• @DJMcMayhem sure. May 31, 2017 at 21:30

## QBIC, 18 bytes

?_s;,:,1|=_sA,-a,1


Explanation

?        =     PRINT -1 if equal, 0 otherwise, between
_s     |      A substring of
;,:,1          A$string (read from cmd line), from the n'th pos, length 1 _sA,-a,1 And a substring of A$, n'th pos from the right, also 1 length
The second Substring is auto-terminated because EOF.


q~_2$=@@~==  Bad. Too much stack manipulation. ## ><> (with this interpreter), 25 bytes i:0(?v ]&=n;>~{:}[:}]&r[}  It doesn't work in TIO: the TIO interpreter doesn't reverse the new stack when doing the [ instruction, but the fish playground does — compare "abcde"5[ooooo; run here and here, for example. The string input is taken from STDIN, and we assume n is already on the stack. Uses 1-indexing. The fish gets the nth character with [:}]&, which siphons off the first n things on the stack into a new, reversed stack, manipulates that a bit, then puts the things back and saves the nth character in the register. It then reverses the whole stack and does the same again, and returns 1 if the two characters are equal, and 0 otherwise. This seems to work at TIO, for 26 bytes: i:0(?v ]&=n;>~{:}[{:}]&r[{  # C, 73 bytes Compiles as-is with GCC 6.3.1 (no flags). Some unnecessary obfuscation included. main(c,v)char**v;{c=atoi(v[2]);putchar((*++v)[c]-(*v)[strlen(*v+1)-c]);}  Usage $./a.out abcdcba 6


Truthy = nothing, falsey = garbage.

# Lua, 35 bytes

Uses 1-indexing.

s,n=...print(s:byte(n)==s:byte(-n))


Try it online!

# C++, 57 Bytes

Assuming header includes don't count.

bool c(std::string s,int i){return s[i]==s[s.size()-i];}

• I'm pretty sure you can remove the spaces at int i){ and s[i]==s[ to make it 54 bytes. Jun 1, 2017 at 9:04
• I don't think this is valid. The first parameter needs to be given an identifier, otherwise you cannot refer to it. But you don't need the space between the closing parenthesis of the function signature and the opening brace of its definition, so this would only increase your code size by 1 byte. You'd get a net decrease by removing the spaces around the equals sign, as Kevin suggested. Jun 1, 2017 at 11:05
• In addition to removing spaces and including s in the parameter list you could also replace bool by int. Jun 1, 2017 at 13:01
• #include<string> int c(std::string s,int i){return s[i]==s[s.size()-i];}(line break of course) is the smallest I get to compile. If you find a compiler that includes <string> on default you may drop it. Jun 1, 2017 at 13:10
• shouldn't it be s[s.size()-i-1] or at least s[s.size()+~i]? Jun 1, 2017 at 13:31

## REXX, 41 bytes

parse arg n,=(n)l+1''-(n)r+1 .
return l=r


Arguments are given in n,s order since n is required for parsing s.

# Crystal, 26 24 Bytes

def c(s,n)s[n]==s[~n]end


-2 Bytes from Felipe Nardi Batista, thanks.

Try it online!

• Welcome to the site! A nice solution, to improve your answer I would recommend adding a "Try it online" link that shows an implementation of your answer. Jun 1, 2017 at 10:09
• s[n]==s[~n] saves 2 bytes Jun 1, 2017 at 11:09
• @Notts90, i don't think there are any online crystal-interpreters. :( The homepage provides one, but I don't think it has an API? Jun 2, 2017 at 7:32
• @Domii look at the try it online link in my previous comment, it's lists crystal as one of the languages it supports. Jun 2, 2017 at 7:51
• @Notts90 Oh sry. I missed that link because the color is almost the same as the rest. Thanks :D Jun 2, 2017 at 22:44

# shortC, 23 bytes

Df(s,n)s[n]==s[Ss)-1-n]


Try it online!

Substitutions in this program:

• D -> #define
• S -> strlen(
• No auto-inserted closing stuff :(

# Perl 5, 23 + 3 (-lF) = 26 bytes

say$F[$n=<>]eq$F[-1-$n]


Try it online!

Uses 0 indexing

# Thunno 2, 3 bytes

Ḳ=i


Try it online!

#### Explanation

Ḳ=i  # Implicit input
Ḳ    # Bifurcate the input string
=   # Check for equality (vectorised)
i  # Index into this list
# Implicit output

• clever! thank you for styling the Try Thunno page better lol. and no i didn’t entirely forget about the docs site, i’ll get that done soonTM Aug 9, 2023 at 18:05
• That's ok. (Also, the restyling was mainly lyxal telling me what code to put where lol :p) Aug 9, 2023 at 18:18

## Javascript, 20 bytes

s=>n=>s[n]==s.at(~n)


# PowerShell, 18 bytes

$a[$b]-eq$a[-$b-1]


Try it online!

# Itr, 6 bytes

ä¡Y=à@

0-indexed

online interpreter

## Explanation

ä       ; duplicate the first (implicit) input
¡      ; reverse it
Y=    ; zip the two lists with equality
à@  ; get the element at the index given by the second index
; implicit output


# Uiua, 5 bytes

⊡:=⇌.


Try it!

⊡:=⇌.
.  # duplicate
⇌   # reverse
=    # elementwise equal
:     # flip
⊡      # pick