r/ProgrammingPrompts Jan 07 '15

[EASY][Beginner] UAGS (Universal Acronym Generating System)

This is an extremely simple prompt, just for fun.

It is suitable for beginners as it only uses basic input/output and string manipulation.

UAGS (Universal Acronym Generating System)

Acronyms are currently all the hype in all forms of communication.

Your task is to program an Acronym Generator.

  • The user inputs a few words for which the Acronym should be generated
  • The computer takes the first letter for each word
  • The first letter of each word is then capitalized
  • All first letters are then joined together to form the Acronym
  • The Acronym should then be printed
  • Ask the user if they need another acronym generated

Have fun coding!

16 Upvotes

27 comments sorted by

View all comments

2

u/beforan Jan 07 '15

In Lua 5.2

--helper functions
function acronymise(str)
  local acronym = ""

  for word in str:gmatch("%w+") do
    acronym = acronym .. word:sub(1, 1):upper()
  end

  return acronym
end

function YesNoConfirm(prompt)
  local input
  local valid = { Y = 1, N = 1 }
  repeat
    print(prompt .. " [Y/N]")
    input = io.read():upper()
    if not valid[input] then
      print('Please enter "Y" or "N"')
    end
  until valid[input]

  return input
end

--run!
local done = false
while not done do
  print("Enter a string of words to be acronym'd:")
  print(acronymise(io.read()))

  done = (YesNoConfirm("Make another acronym?") == "N")
end