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

Published on 12 May 2024 09:40 PM

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

Table of Contents

Problem Statement

Mad Libs is a phrasal template word game where one player prompts another for a list of words to substitute for blanks in a story, usually with funny results.

Write a program to create a Mad Libs like story. The program should read an arbitrary multiline story from input. The story will be terminated with a blank line. Then, find each replacement to be made within the story, ask the user for a word to replace it with, and make all the replacements. Stop when there are none left and print the final story.

The input should be an arbitrary story in the form: Given this example, it should then ask for a name, a he or she and a noun ( gets replaced both times with the same value).

Let's start with the solution:

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

Source code in the pl/i programming language

(stringrange, stringsize):                            /* 2 Nov. 2013 */
Mad_Libs: procedure options (main);
   declare (line, left, right) character (100) varying;
   declare true bit(1) value ('1'b), false bit (1) value ('0'b);
   declare name    character (20) varying, seen_name    bit (1) initial (false);
   declare pronoun character (20) varying, seen_pronoun bit (1) initial (false);
   declare noun    character (20) varying, seen_noun    bit (1) initial (false);
   declare replaced_all bit (1);
   declare in file input;

   open file (in) title ('/MADLIBS.DAT,type(text),recsize(100)');

   do forever;
      get file (in) edit (line) (L);
      if line = '' then leave;

      do until (replaced_all);
         replaced_all = true;
         if index(line, '') > 0 then
            if seen_name then
               do until (index(line, '') = 0);
                  call split(line, '', left, right);
                  line = left || name || right;
                  replaced_all = false;
               end;
            else
               do;
                  put skip list ('Please type a name:');
                  get edit (name) (L);
                  seen_name = true; replaced_all = false;
               end;
         if index(line, '') > 0 then
            if seen_pronoun then
               do until (index(line, '') = 0);
                  call split(line, '', left, right);
                  line = left || pronoun || right;
                  replaced_all = false;
               end;
            else
               do;
                   put skip list ('Please type a pronoun (he or she):');
                   get edit (pronoun) (L);
                   seen_pronoun = true; replaced_all = false;
               end;
         if index(line, '') > 0 then
            if seen_noun then
               do until (index(line, '') = 0);
                  call split(line, '', left, right);
                  line = left || noun || right;
                  replaced_all = false;
               end;
            else
               do;
                  put skip list ('Please type a noun:');
                  get edit (noun) (L);
                  seen_noun = true; replaced_all = false;
               end;
         end;
      put skip list (line);
   end;

split: procedure (line, text, Left, Right);
   declare (line, text, left, right) character (*) varying;
   declare i fixed binary;

   i = index(line, text);
   left  = substr(line, 1, i-1);
   right = substr(line, i+length(text), length(line) - (i + length(text)) + 1 );
end split;

end Mad_Libs;

  

You may also check:How to resolve the algorithm Show the epoch step by step in the PL/I programming language
You may also check:How to resolve the algorithm Loops/For step by step in the PL/I programming language
You may also check:How to resolve the algorithm AKS test for primes step by step in the PL/I programming language
You may also check:How to resolve the algorithm Loop over multiple arrays simultaneously step by step in the PL/I programming language
You may also check:How to resolve the algorithm Doubly-linked list/Definition step by step in the PL/I programming language