How to resolve the algorithm Mad Libs step by step in the Tcl programming language
Published on 12 May 2024 09:40 PM
How to resolve the algorithm Mad Libs step by step in the Tcl 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 (
Let's start with the solution:
Step by Step solution about How to resolve the algorithm Mad Libs step by step in the Tcl programming language
Source code in the tcl programming language
package require Tcl 8.5
# Read the template...
puts [string repeat "-" 70]
puts "Enter the story template, ending with a blank line"
while {[gets stdin line] > 0} {
append content $line "\n"
}
# Read the mapping...
puts [string repeat "-" 70]
set mapping {}
foreach piece [regexp -all -inline {<[^>]+>} $content] {
if {[dict exists $mapping $piece]} continue
puts -nonewline "Give me a $piece: "
flush stdout
dict set mapping $piece [gets stdin]
}
# Apply the mapping and print...
puts [string repeat "-" 70]
puts -nonewline [string map $mapping $content]
puts [string repeat "-" 70]
You may also check:How to resolve the algorithm System time step by step in the LIL programming language
You may also check:How to resolve the algorithm Seven-sided dice from five-sided dice step by step in the Raku programming language
You may also check:How to resolve the algorithm Closest-pair problem step by step in the Groovy programming language
You may also check:How to resolve the algorithm Miller–Rabin primality test step by step in the J programming language
You may also check:How to resolve the algorithm Arrays step by step in the Clean programming language