program DollarSymbolValidator;
function IsLetter(c: Char): Boolean;
begin
IsLetter := ((c >= 'a') and (c <= 'z')) or ((c >= 'A') and (c <= 'Z'));
end;
function RemoveDollarWordPatterns(const input: string): string;
var
i, j: Integer;
inPattern, allLetters: Boolean;
currentWord: string;
result_str: string;
begin
result_str := '';
inPattern := False;
currentWord := '';
i := 1;
while i <= Length(input) do
begin
if input[i] = '$' then
begin
if not inPattern then
begin
// Start of potential pattern
inPattern := True;
currentWord := '';
end
else
begin
// End of pattern - check if currentWord contains only letters
if (Length(currentWord) > 0) then
begin
// Check if all characters in currentWord are letters
allLetters := True;
for j := 1 to Length(currentWord) do
begin
if not IsLetter(currentWord[j]) then
begin
allLetters := False;
break;
end;
end;
if not allLetters then
begin
// Not a valid pattern, add the opening $ and the word
result_str := result_str + '$' + currentWord + '$';
end;
// If it's a valid pattern, we skip it (don't add to result)
end
else
begin
// Empty pattern $$, add both dollars
result_str := result_str + '$$';
end;
inPattern := False;
currentWord := '';
end;
end
else
begin
if inPattern then
begin
currentWord := currentWord + input[i];
end
else
begin
result_str := result_str + input[i];
end;
end;
Inc(i);
end;
// Handle case where string ends while in pattern
if inPattern then
begin
result_str := result_str + '$' + currentWord;
end;
RemoveDollarWordPatterns := result_str;
end;
function IncludeDollarSymbolText(const input: string): Boolean;
var
text: string;
i: Integer;
begin
// Remove valid $word$ patterns
text := RemoveDollarWordPatterns(input);
// Search for remaining dollar symbols
for i := 1 to Length(text) do
begin
if text[i] = '$' then
begin
IncludeDollarSymbolText := False;
Exit;
end;
end;
IncludeDollarSymbolText := True;
end;
function BoolToStr(b: Boolean): string;
begin
if b then
BoolToStr := 'true'
else
BoolToStr := 'false';
end;
begin
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $text$ z'))); // ok
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $ text$ z'))); // space
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $$ z'))); // empty
WriteLn(BoolToStr(IncludeDollarSymbolText('abc 100 $text$ z'))); // ok
WriteLn(BoolToStr(IncludeDollarSymbolText('abc $1000 $text$ z'))); // open $
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $IBM$ z $Microsoft$'))); // ok
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $F3$ z'))); // include number
WriteLn(BoolToStr(IncludeDollarSymbolText('abc xy $text z'))); // missing close $
end.
(*
run:
true
false
false
true
false
true
false
false
*)