Mad Libs: Difference between revisions

2,489 bytes added ,  9 years ago
Add Batch File Solution
(→‎{{header|Fortran}}: Escalate to laying out the output.)
(Add Batch File Solution)
Line 402:
found a flower. Barbara decided to take it home.
</pre>
 
=={{header|Batch File}}==
This implementation takes a file from command line. This will not validate the input except when the user inputs word/s surrounded by angle brackets. For example, if the user inputs:
<pre>Enter a value for [Sample Variable]? <blabla> </pre>
Then, the term 'blabla' will interpreted as blank...
<lang dos>::Save this as MADLIBS.BAT
 
@echo off
setlocal enabledelayedexpansion
%== Check if there is no arguments ==%
if "%~1"=="" (
echo.
echo.[Mad Libs - Batch File Implementation]
echo.
echo.Usage: MADLIBS [file]
echo.
exit /b 1
)
if not exist "%~f1" (echo.File not found.&exit /b 1)
 
echo.
%== Read the text file ==%
echo.[Mad Libs - Batch File Implementation]
echo.
for /f "tokens=* eol=_" %%A in (%~f1) do (
set /a cnt+=1
set "line!cnt!=%%A"
)
%== User input the missing parts ==%
echo.
for /l %%. in (1,1,!cnt!) do (
call :proc_line "!line%%.!"
)
%== Display the edited story... ==%
echo.
echo.The Story:
echo.
for /l %%? in (1,1,!cnt!) do (
echo. !line%%?!
)
echo.
exit /b 0
 
%== The main processor of the story ==%
:proc_line
set "str=%~1"
:loop
if "!str!"=="" goto :EOF
for /f "tokens=1,* delims=<" %%M in ("!str!") do (
for /f "tokens=1,* delims=>" %%X in ("%%M") do (
if not "%%M"=="%%X" (
set "temp_var=%%X"
if not "!temp_var: =!"=="" (
set "input="
set /p "input=Enter a value for [%%X]?"
call :subst_input
)
)
)
set "str=%%N"
)
goto loop
 
%== This Substitutes the input to the blank ==%
:subst_input
set "chk_brack=!input:>=!"
set "chk_brack=!chk_brack:<=!"
for /l %%. in (1,1,!cnt!) do (
if "!chk_brack!"=="!input!" (
call set "line%%.=%%line%%.:<%%X>=!input!%%"
) else (set "line%%.=!line%%.:<%%X>=!")
)
goto :EOF</lang>
Sample text file:(text1.txt)
<pre>_Sample Mad Libs Story...
_
_NOTES:
_ If you want to put an exclamation('!'), put a caret before it (e.g.'^!')
_ If there is an underscore before a line like THIS, that will be ignored...
_
_ Please do not make "unbalanced" angle brackets.
_
_ Remember that "< samp>" (w/ a leading whitespace) and "<samp>" (w/o)
_ will be interpreted DIFFERENTLY...
 
<name> went for a walk in the park. <he or she>
found a <noun>. <name> decided to take it home^!^!^!</pre>
{{Out}}
<pre>>madlibs story1.txt
 
[Mad Libs - Batch File Implementation]
 
 
Enter a value for [name]?Lisa
Enter a value for [he or she]?she
Enter a value for [noun]?bag
 
The Story:
 
Lisa went for a walk in the park. she
found a bag. Lisa decided to take it home!!!
 
></pre>
 
== {{header|C}} ==
535

edits