program PatternMatch;
const
MaxWords = 50;
type
TStringArray = array[1..MaxWords] of string;
function ToLower(ch: char): char;
begin
if (ch >= 'A') and (ch <= 'Z') then
ToLower := chr(ord(ch) + 32)
else
ToLower := ch;
end;
function SplitWords(sentence: string; var words: TStringArray): integer;
var
i, count, start: integer;
begin
count := 0;
start := 1;
for i := 1 to length(sentence) + 1 do
begin
if (i > length(sentence)) or (sentence[i] = ' ') then
begin
if (i > start) then
begin
inc(count);
words[count] := copy(sentence, start, i - start);
end;
start := i + 1;
end;
end;
SplitWords := count;
end;
function MatchesPattern(pattern, sentence: string): boolean;
var
words: TStringArray;
wordCount, i: integer;
begin
wordCount := SplitWords(sentence, words);
if length(pattern) <> wordCount then
begin
MatchesPattern := false;
exit;
end;
for i := 1 to wordCount do
begin
if ToLower(pattern[i]) <> ToLower(words[i][1]) then
begin
MatchesPattern := false;
exit;
end;
end;
MatchesPattern := true;
end;
var
pattern, sentence: string;
begin
pattern := 'jpcrg';
sentence := 'java python c rust go';
if MatchesPattern(pattern, sentence) then
writeln('Pattern matches!')
else
writeln('Pattern does NOT match.');
end.
(*
run:
Pattern matches!
*)