# 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) – Stephen May 31 '17 at 20:50
• Would it be acceptable to take n as a code-point? (for esoteric languages such as brain-flak) – DJMcMayhem May 31 '17 at 21:29
• @DJMcMayhem sure. – Stephen May 31 '17 at 21:30

# 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. – Kevin Cruijssen Jun 1 '17 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. – Cody Gray Jun 1 '17 at 11:05
• In addition to removing spaces and including s in the parameter list you could also replace bool by int. – sigvaldm Jun 1 '17 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. – Christoph Jun 1 '17 at 13:10
• shouldn't it be s[s.size()-i-1] or at least s[s.size()+~i]? – Felipe Nardi Batista Jun 1 '17 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. – Notts90 Jun 1 '17 at 10:09
• s[n]==s[~n] saves 2 bytes – Felipe Nardi Batista Jun 1 '17 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? – Domii Jun 2 '17 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. – Notts90 Jun 2 '17 at 7:51
• @Notts90 Oh sry. I missed that link because the color is almost the same as the rest. Thanks :D – Domii Jun 2 '17 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