How to resolve the algorithm Semordnilap step by step in the PL/I programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Semordnilap step by step in the PL/I programming language

Table of Contents

Problem Statement

A semordnilap is a word (or phrase) that spells a different word (or phrase) backward. "Semordnilap" is a word that itself is a semordnilap. Example: lager and regal

This task does not consider semordnilap phrases, only single words. Using only words from this list, report the total number of unique semordnilap pairs, and print 5 examples. Two matching semordnilaps, such as lager and regal, should be counted as one unique pair. (Note that the word "semordnilap" is not in the above dictionary.)

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Semordnilap step by step in the PL/I programming language

Source code in the pl/i programming language

find: procedure options (main); /* 20/1/2013 */
   declare word character (20) varying controlled;
   declare dict(*) character (20) varying controlled;
   declare 1 pair controlled,
              2 a character (20) varying, 2 b character (20) varying;
   declare (i, j) fixed binary;
   declare in file;

   open file(in) title ('/UNIXDICT.TXT,type(LF),recsize(100)');
   on endfile (in) go to completed_read;
   do forever;
      allocate word;
      get file (in) edit (word) (L);
   end;

completed_read:
   free word; /* because at the final allocation, no word was stored. */
   allocate dict(allocation(word));
   do i = 1 to hbound(dict,1);
      dict(i) = word; free word;
   end;

   /* Search dictionary for pairs: */
   do i = 1 to hbound(dict,1)-1;
      do j = i+1 to hbound(dict,1);
         if length(dict(i)) = length(dict(j)) then
            do;
               if dict(i) = reverse(dict(j)) then
                  do;
                     allocate pair; pair.a = dict(i); pair.b = dict(j);
                  end;
            end;
      end;
   end;

   put skip list ('There are ' || trim(allocation(pair)) || ' pairs.');

   do while (allocation(pair) > 0);
      put skip edit (pair) (a, col(20), a); free pair;
   end;
end find;

  

You may also check:How to resolve the algorithm Forward difference step by step in the PL/I programming language
You may also check:How to resolve the algorithm Text processing/1 step by step in the PL/I programming language
You may also check:How to resolve the algorithm Associative array/Creation step by step in the PL/I programming language
You may also check:How to resolve the algorithm Bitwise IO step by step in the PL/I programming language
You may also check:How to resolve the algorithm Flipping bits game step by step in the PL/I programming language