Welcome to collectivesolver - Programming & Software Q&A with code examples. A website with trusted programming answers. All programs are tested and work.

Contact: aviboots(AT)netvision.net.il

Buy a domain name - Register cheap domain names from $0.99 - Namecheap

Scalable Hosting That Grows With You

Secure & Reliable Web Hosting, Free Domain, Free SSL, 1-Click WordPress Install, Expert 24/7 Support

Semrush - keyword research tool

Boost your online presence with premium web hosting and servers

Disclosure: My content contains affiliate links.

39,847 questions

51,768 answers

573 users

How to check if a string includes $sometext$ without numbers in Pascal

1 Answer

0 votes
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
  
*)

 



answered Jul 11, 2025 by avibootz
...