Palindrome detection: Difference between revisions

Add CLU
(Added solution for Action!)
(Add CLU)
Line 1,240:
false
</pre>
 
=={{header|CLU}}==
<lang clu>% Reverse a string
str_reverse = proc (s: string) returns (string)
chs: array[char] := array[char]$predict(0, string$size(s))
for c: char in string$chars(s) do
array[char]$addl(chs, c)
end
return (string$ac2s(chs))
end str_reverse
 
% 'Normalize' a string (remove everything but letters and make uppercase)
normalize = proc (s: string) returns (string)
chs: array[char] := array[char]$predict(0, string$size(s))
for c: char in string$chars(s) do
if c>='a' cand c<='z' then
c := char$i2c(char$c2i(c) - 32)
end
if c>='A' cand c<='Z' then
array[char]$addh(chs, c)
end
end
return (string$ac2s(chs))
end normalize
 
% Check if a string is an exact palindrome
palindrome = proc (s: string) returns (bool)
return (s = str_reverse(s))
end palindrome
 
% Check if a string is an inexact palindrome
inexact_palindrome = proc (s: string) returns (bool)
return (palindrome(normalize(s)))
end inexact_palindrome
 
% Test cases
start_up = proc ()
po: stream := stream$primary_output()
tests: array[string] := array[string]$[
"rotor", "racecar", "RACEcar", "level", "rosetta",
"A man, a plan, a canal: Panama",
"Egad, a base tone denotes a bad age",
"This is not a palindrome"
]
for test: string in array[string]$elements(tests) do
stream$puts(po, "\"" || test || "\": ")
if palindrome(test) then
stream$putl(po, "exact palindrome")
elseif inexact_palindrome(test) then
stream$putl(po, "inexact palindrome")
else
stream$putl(po, "not a palindrome")
end
end
end start_up</lang>
{{out}}
<pre>"rotor": exact palindrome
"racecar": exact palindrome
"RACEcar": inexact palindrome
"level": exact palindrome
"rosetta": not a palindrome
"A man, a plan, a canal: Panama": inexact palindrome
"Egad, a base tone denotes a bad age": inexact palindrome
"This is not a palindrome": not a palindrome</pre>
 
=={{header|COBOL}}==
2,114

edits