Sure. It uses a hash table to keep track of which characters occur in
each file. The hash table is 256 bits initialized to zero. When it
encounters a character in file 1 it sets the corresponding bit in the
hash table. It does that by taking the 3 low order bits as the index
to the hash table. Those bits will fall in the range 0..7. The high
order bits, obtained by shifting the character right 3 bits, will give
a value in the range 0..32. We shift a bit into that location and use
bitwise or to set the bit in the hash table. We do that for both
files. The final "for" loop checks each character to determine if its
bit is set in both hash tables. If so, it occurs in both places and we
output that character.

The code would be somewhat simpler if I didn't try to use every bit in
the hash table. It would take 512 bytes of memory instead of 512 bits.

int main(int argc, char *argv[])
{
  FILE *f1 = fopen(argv[1],"r");
  FILE *f2 = fopen(argv[2],"r");
  char hash1[256] = {0};
  char hash2[256] = {0};
  char ch;

  while((ch=getc(f1)) != EOF)
      hash1[ch] = 1;

  while((ch=getc(f2)) != EOF)
      hash2[ch] = 1;

  for(ch = 0; ch < 256; ++ch)
    if (hash1[ch] && hash2[ch])
      printf("%c", ch);

  return 0;
}

Don

On Aug 25, 11:42 am, Shrey Choudhary <choudharyshre...@gmail.com>
wrote:
> @Don..
>
> Can you briefly explain the program?

-- 
You received this message because you are subscribed to the Google Groups 
"Algorithm Geeks" group.
To post to this group, send email to algogeeks@googlegroups.com.
To unsubscribe from this group, send email to 
algogeeks+unsubscr...@googlegroups.com.
For more options, visit this group at 
http://groups.google.com/group/algogeeks?hl=en.

Reply via email to