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,845 questions

51,766 answers

573 users

How to get the number of characters that two strings have in common in Pascal

2 Answers

0 votes
program CommonCharactersCountProgram;

function CommonCharactersCount(str1, str2: string): Integer;
var
  set1, set2: set of Char;
  c: Char;
  commonCount: Integer;
begin
  set1 := [];
  set2 := [];
  
  // Populate set1 with characters from str1
  for c in str1 do
    set1 := set1 + [c];
    
  // Populate set2 with characters from str2
  for c in str2 do
    set2 := set2 + [c];
    
  // Find the intersection of set1 and set2 and count common characters
  commonCount := 0;
  for c in set1 do
    if c in set2 then
      Inc(commonCount);
      
  CommonCharactersCount := commonCount;
end;

var
  str1, str2: string;
begin
  str1 := 'abcdefg';
  str2 := 'xayzgoe';
  
  WriteLn(CommonCharactersCount(str1, str2)); 
end.



(*
run:

3

*)

 



answered Mar 20, 2025 by avibootz
0 votes
program CommonCharactersCountProgram;

function CountCommonCharacters(str1, str2: string): Integer;
var
  i, j: Integer;
  commonCount: Integer;
begin
  commonCount := 0;
  for i := 1 to Length(str1) do
  begin
    for j := 1 to Length(str2) do
    begin
      if str1[i] = str2[j] then
      begin
        Inc(commonCount);
        Break; // Move to the next character in str1
      end;
    end;
  end;
  CountCommonCharacters := commonCount;
end;

var
  str1, str2: string;
  commonChars: Integer;
begin
  str1 := 'abcdefg';
  str2 := 'xayzgoe';

  commonChars := CountCommonCharacters(str1, str2);
  
  WriteLn('Number of common characters: ', commonChars);
end.



(*
run:

Number of common characters: 3

*)

 



answered Mar 20, 2025 by avibootz
...