Sign in Join
Wiki copytemporary

Palindrome: what changed

Revision 0 to 1. Removed lines have a minus sign; added lines have a plus sign.

Title: Palindrome

OldNewLine
1+<figure>
2+<img src="fpc_source_logo.png" title="fpc_source_logo.png" />
3+<figcaption>fpc_source_logo.png</figcaption>
4+</figure>
5+
6+# Palindrome
7+
8+A palindrome is a word, phrase, number, or other sequence of symbols or elements, whose meaning may be interpreted the same way in either forward or reverse direction
9+
10+# Examples
11+
12+## English
13+
14+- racecar
15+- A man, a plan, a canal: Panama
16+- Madam I'm Adam
17+
18+## Arabic
19+
20+- مودته تدوم لكل هول * وهل كل مودته تدوم
21+
22+## Armenian
23+
24+- Արա իմ, առած դրամ՝ քաղաքեքաղաք մարդ ծառա մի արա
25+- Սերամա՜հ եթե համարես
26+
27+## Finnish
28+
29+- saippuakivikauppias
30+- No niin, on aamu. Kehua sinua kai saan, ah, ihana Kristian. Nait Sirkan, ah, ihana asia, kaunis. Au, hekumaa. No niin on.
31+- No, oikotie vei Tokioon.
32+
33+## Russian
34+
35+- А роза упала на лапу Азора
36+- луну дунул
37+- лак резала зеркал
38+
39+# Function IsPalindome
40+
41+This function is suitable for both [[ASCII]] and [[UTF-8]] encoding.
42+
43+``` pascal
44+function IsPalindome(const a_text: string): boolean;
45+var
46+ i, j :longint;
47+ s1, s2, s3 :string;
48+begin
49+ s1 := UTF8LowerCase( a_text );
50+ j := length( s1 );
51+ i := 1;
52+ s2 := '';
53+ s3 := '';
54+ while ( i <= j ) do
55+ begin
56+ if s1[i] < #$C0 then // ASCII
57+ begin
58+ if (s1[i] >= #$30) and (s1[i] < #$7B)then
59+ begin
60+ if (s1[i] > #$60) or (s1[i] < #$3A) then
61+ begin
62+ s2 := s1[i] + s2;
63+ s3 := s3 + s1[i];
64+ end;
65+ end;
66+ inc( i );
67+ end
68+ else
69+ begin
70+ begin
71+ if s1[i] < #$E0 then // two byte
72+ begin
73+ if ((s1[i] > #$C2) and ( not
74+ // Armenian punctuation
75+ ((s1[i] = #$D5) and ((s1[i+1] >= #$99) and (s1[i+1] < #$A0)))
76+ )) then
77+ begin
78+ s2 := s1[i]+ s1[i+1] + s2;
79+ s3 := s3 + s1[i] + s1[i+1];
80+ end;
81+ inc( i ,2);
82+ end
83+ else
84+ begin
85+ if s1[i] < #$F0 then // three byte
86+ begin
87+ if not (
88+ // General punctuation
89+ (s1[i] = #$E2) and ((s1[i+1]= #$80)
90+ or ((s1[i+1]= #$81) and (s1[i+2]< #$B0)))
91+ ) then
92+ begin
93+ s2 := s1[i]+ s1[i+1] + s1[i+2]+ s2;
94+ s3 := s3 + s1[i] + s1[i+1] + s1[i+2];
95+ end;
96+ inc( i ,3);
97+ end
98+ else
99+ begin
100+ s2 := s1[i]+ s1[i+1] + s1[i+2]+ s1[i+3] + s2;
101+ s3 := s3 + s1[i] + s1[i+1] + s1[i+2] + s1[i+3];
102+ inc( i ,4);
103+ end
104+ end;
105+ end;
106+ end;
107+ end;
108+ if s2 <> '' then result := s2=s3 else result := false;
109+end;
110+```
111+
112+---
113+*From the Free Pascal wiki, page "Palindrome", as it stood on 2020-02-23 after a change by Trev, out of the wiki's dump of 2024-05-26. Copyright stays with the author who wrote it, according to the old wiki's [[Licensing]] page. If you have concerns about this material, [message the admin](/wiki-contact).*

Back to history