aboutsummaryrefslogtreecommitdiff
path: root/challenge-330/roger-bell-west/lua/ch-2.lua
blob: 06b0d0875de821979dea65ae92ebf0d9def83b06 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
#! /usr/bin/lua

function split(t)
   local cl = {}
   string.gsub(t,
               "(.)",
               function(c)
                  table.insert(cl, c)
               end
   )
   return cl
end

function splits(inputstr, sep)
   sep=sep or '%s'
   local t={}
   for field,s in string.gmatch(inputstr, "([^"..sep.."]*)("..sep.."?)") do
      table.insert(t,field)
      if s=="" then
         return t
      end
   end
end

function join(t)
   local out=""
   for i, v in ipairs(t) do
      out = out .. v
   end
   return out
end

function joins(t,pad)
   local out=""
   local later = false
   for k,v in pairs(t) do
      if later then
         out = out .. pad
      end
      out = out .. v
      later = true
   end
   return out
end

function titlecapital(a)
   local out = {}
   for _, w in ipairs(splits(a, " ")) do
      local p = string.lower(w)
      if #p > 2 then
         local c = split(p)
         c[1] = string.upper(c[1])
         p = join(c)
      end
      table.insert(out, p)
   end
   return joins(out, " ")
end

if titlecapital("PERL IS gREAT") == "Perl is Great" then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if titlecapital("THE weekly challenge") == "The Weekly Challenge" then
  io.write("Pass")
else
  io.write("FAIL")
end
io.write(" ")

if titlecapital("YoU ARE A stAR") == "You Are a Star" then
  io.write("Pass")
else
  io.write("FAIL")
end
print("")