Palindrome detection: Difference between revisions

Content deleted Content added
Thundergnat (talk | contribs)
Rename Perl 6 -> Raku, alphabetize, minor clean-up
Line 92:
return true;
}</lang>
 
=={{header|Ada}}==
<lang ada>function Palindrome (Text : String) return Boolean is
Line 147 ⟶ 148:
sequence "ingirumimusnocteetconsumimurigni" is a palindrome
</pre>
 
=={{header|APL}}==
NARS2000 APL, dynamic function "if the argument matches the reverse of the argument", with Unicode character support:
Line 165 ⟶ 167:
1</lang>
Dyalog APL has a Unicode-aware uppercase/lowercase function (819 I-beam), AFAIK no support for looking up Unicode character classes.
 
=={{header|AppleScript}}==
 
Line 395 ⟶ 398:
Not a palindrome.
</pre>
 
=={{header|Bash}}==
<lang bash>
#! /bin/bash
# very simple way to detect a palindrome in Bash
# output of bash --version -> GNU bash, version 4.4.7(1)-release x86_64 ...
 
echo "enter a string"
read input
 
size=${#input}
count=0
 
while (($count < $size))
do
array[$count]=${input:$count:1}
(( count+=1 ))
done
 
count=0
 
for ((i=0 ; i < $size; i+=1))
do
if [ "${array[$i]}" == "${array[$size - $i - 1]}" ]
then
(( count += 1 ))
fi
done
 
if (( $count == $size ))
then
echo "$input is a palindrome"
fi
</lang>
 
=={{header|BASIC}}==
Line 566 ⟶ 603:
{{out}}
<pre>"A man, a plan, a canal: Panama!" is a palindrome</pre>
 
=={{header|Bash}}==
<lang bash>
#! /bin/bash
# very simple way to detect a palindrome in Bash
# output of bash --version -> GNU bash, version 4.4.7(1)-release x86_64 ...
 
echo "enter a string"
read input
 
size=${#input}
count=0
 
while (($count < $size))
do
array[$count]=${input:$count:1}
(( count+=1 ))
done
 
count=0
 
for ((i=0 ; i < $size; i+=1))
do
if [ "${array[$i]}" == "${array[$size - $i - 1]}" ]
then
(( count += 1 ))
fi
done
 
if (( $count == $size ))
then
echo "$input is a palindrome"
fi
</lang>
 
=={{header|Batch File}}==
Line 793 ⟶ 796:
t, palindrome_r(t, 0, l) ? "" : "n't");
return 0;
}</lang>
 
=={{header|C++}}==
The C solutions also work in C++, but C++ allows a simpler one:
<lang cpp>#include <string>
#include <algorithm>
 
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.end(), s.rbegin());
}</lang>
 
Or, checking half is sufficient (on odd-length strings, this will ignore the middle element):
<lang cpp>#include <string>
#include <algorithm>
 
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}</lang>
 
Line 877 ⟶ 861:
Console.WriteLine("ingirumimusnocteetconsumimurigni".IsPalindrome());
}
}</lang>
 
=={{header|C++}}==
The C solutions also work in C++, but C++ allows a simpler one:
<lang cpp>#include <string>
#include <algorithm>
 
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.end(), s.rbegin());
}</lang>
 
Or, checking half is sufficient (on odd-length strings, this will ignore the middle element):
<lang cpp>#include <string>
#include <algorithm>
 
bool is_palindrome(std::string const& s)
{
return std::equal(s.begin(), s.begin()+s.length()/2, s.rbegin());
}</lang>
 
Line 1,053 ⟶ 1,056:
imperative 32.8M ( 30.49ns) (± 3.29%) 0 B/op fastest
</lang>
 
=={{header|Delphi}}==
<lang Delphi>uses
SysUtils, StrUtils;
 
function IsPalindrome(const aSrcString: string): Boolean;
begin
Result := SameText(aSrcString, ReverseString(aSrcString));
end;</lang>
 
=={{header|D}}==
Line 1,193 ⟶ 1,187:
}
</lang>
 
=={{header|Delphi}}==
<lang Delphi>uses
SysUtils, StrUtils;
 
function IsPalindrome(const aSrcString: string): Boolean;
begin
Result := SameText(aSrcString, ReverseString(aSrcString));
end;</lang>
 
=={{header|Dyalect}}==
 
<lang dyalect>func isPalindrom(str) {
str == str.reverse()
}
 
print(isPalindrom("ingirumimusnocteetconsumimurigni"))</lang>
 
=={{header|Déjà Vu}}==
Line 1,210 ⟶ 1,221:
<pre>true
false</pre>
 
=={{header|Dyalect}}==
 
<lang dyalect>func isPalindrom(str) {
str == str.reverse()
}
 
print(isPalindrom("ingirumimusnocteetconsumimurigni"))</lang>
 
=={{header|E}}==
Line 1,299 ⟶ 1,302:
true
</pre>
 
=={{header|Elm}}==
<lang elm>import String exposing (reverse, length)
Line 1,403 ⟶ 1,407:
<lang factor>USING: kernel sequences ;
: palindrome? ( str -- ? ) dup reverse = ;</lang>
 
 
=={{header|Falcon}}==
Line 1,747 ⟶ 1,750:
return t = Reversed(t);
end;</lang>
 
=={{header|GML}}==
<lang go>
Line 2,350 ⟶ 2,354:
palindro(`ingirumimusnocteetconsumimurigni')
palindro(`this is not palindrome')</lang>
 
 
=={{header|Maple}}==
Line 2,845 ⟶ 2,848:
 
and to make the test case insensitive, just use the function <tt>String.lowercase_ascii</tt>.
 
=={{header|Oforth}}==
 
<lang Oforth>String method: isPalindrome self reverse self == ;</lang>
 
=={{header|Octave}}==
Line 2,875 ⟶ 2,874:
<lang octave>palindro_r("ingirumimusnocteetconsumimurigni")
palindro("satorarepotenetoperarotas")</lang>
 
=={{header|Oforth}}==
 
<lang Oforth>String method: isPalindrome self reverse self == ;</lang>
 
=={{header|Ol}}==
Line 3,079 ⟶ 3,082:
(and too fast for a reliable count).
The Perl regular expression engine recursed twice as fast as the Perl interpreter.
 
=={{header|Perl 6}}==
<lang perl6>subset Palindrom of Str where {
.flip eq $_ given .comb(/\w+/).join.lc
}
my @tests = q:to/END/.lines;
A man, a plan, a canal: Panama.
My dog has fleas
Madam, I'm Adam.
1 on 1
In girum imus nocte et consumimur igni
END
for @tests { say $_ ~~ Palindrom, "\t", $_ }</lang>
{{out}}
<pre>True A man, a plan, a canal: Panama.
False My dog has fleas
True Madam, I'm Adam.
False 1 on 1
True In girum imus nocte et consumimur igni
</pre>
 
=={{header|Phix}}==
Line 3,241 ⟶ 3,222:
NEXT
END FUNCTION</lang>
 
 
=={{header|PowerShell}}==
Line 3,481 ⟶ 3,461:
<lang prolog>pali(Str) :- sub_atom(Str, 0, 1, _, X), atom_concat(Str2, X, Str), atom_concat(X, Mid, Str2), pali(Mid).
pali(Str) :- atom_length(Str, Len), Len < 2.</lang>
 
=={{header|PureBasic}}==
{{works with|PureBasic|4.41}}
Line 3,661 ⟶ 3,642:
>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<lang perl6>subset Palindrom of Str where {
.flip eq $_ given .comb(/\w+/).join.lc
}
my @tests = q:to/END/.lines;
A man, a plan, a canal: Panama.
My dog has fleas
Madam, I'm Adam.
1 on 1
In girum imus nocte et consumimur igni
END
for @tests { say $_ ~~ Palindrom, "\t", $_ }</lang>
{{out}}
<pre>True A man, a plan, a canal: Panama.
False My dog has fleas
True Madam, I'm Adam.
False 1 on 1
True In girum imus nocte et consumimur igni
</pre>
 
=={{header|Rascal}}==
Line 4,083 ⟶ 4,087:
}
}</lang>
 
=={{header|Simula}}==
<lang simula>BEGIN
Line 4,218 ⟶ 4,223:
<lang sql>SET @txt = REPLACE('In girum imus nocte et consumimur igni', ' ', '');
SELECT REVERSE(@txt) = @txt;</lang>
 
=={{header|Swift}}==
{{works with|Swift|1.2}}