How to use a set of characters in Pascal

1 Answer

0 votes
program SetOfCharProgram;

var
  mySet: set of Char;
  ch: Char;
  count: Integer;
  firstChar: Char;
begin
  //mySet := ['A', 'B', 'C', 'A']; // Error: Duplicate 'A'
  mySet := ['A', 'B', 'C', 'D'];

  Write('Set contains: ');
  for ch := 'A' to 'Z' do
    if ch in mySet then
      Write(ch, ' ');
  WriteLn;

  // Print first element
  count := 0;
  for ch := 'A' to 'Z' do
    if ch in mySet then
    begin
      Inc(count);
      if count = 1 then
      begin
        firstChar := ch;
        WriteLn('First element in set: ', firstChar);
      end;
      if count = 2 then
      begin
        // Change second element
        Exclude(mySet, ch);   // Remove original second
        Include(mySet, 'Z');  // Add new character
      end;
    end;

  // Print updated set
  Write('Updated set contains: ');
  for ch := 'A' to 'Z' do
    if ch in mySet then
      Write(ch, ' ');
  WriteLn;
end.


  
  
(*
run:
 
Set contains: A B C D 
First element in set: A
Updated set contains: A C D Z 
 
*)

 



answered Jul 5, 2025 by avibootz

Related questions

...