diff options
| author | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
|---|---|---|
| committer | Paulo Custodio <pauloscustodio@gmail.com> | 2021-01-26 21:40:08 +0000 |
| commit | 536dafb989fad8da4c2df0df1bfc22eb2fac3706 (patch) | |
| tree | c2b39c685cca7cc2ed3b4ab4e91e75f5b9a71fdd /challenge-096/paulo-custodio/lua/ch-1.lua | |
| parent | e36daeee6e93355383ad3a1c3fc43271f9a357d7 (diff) | |
| parent | c34bb5d7bd7fce08e8311a0f527ce7fbd69e4dae (diff) | |
| download | perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.gz perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.tar.bz2 perlweeklychallenge-club-536dafb989fad8da4c2df0df1bfc22eb2fac3706.zip | |
Merge remote-tracking branch 'upstream/master'
Diffstat (limited to 'challenge-096/paulo-custodio/lua/ch-1.lua')
| -rw-r--r-- | challenge-096/paulo-custodio/lua/ch-1.lua | 30 |
1 files changed, 30 insertions, 0 deletions
diff --git a/challenge-096/paulo-custodio/lua/ch-1.lua b/challenge-096/paulo-custodio/lua/ch-1.lua new file mode 100644 index 0000000000..a278af7a23 --- /dev/null +++ b/challenge-096/paulo-custodio/lua/ch-1.lua @@ -0,0 +1,30 @@ +#!/usr/bin/env lua + +--[[ +Challenge 096 + +TASK #1 > Reverse Words +Submitted by: Mohammad S Anwar +You are given a string $S. + +Write a script to reverse the order of words in the given string. The string +may contain leading/trailing spaces. The string may have more than one space +between words in the string. Print the result without leading/trailing spaces +and there should be only one space between words. + +Example 1: +Input: $S = "The Weekly Challenge" +Output: "Challenge Weekly The" +--]] + +-- concatenate all words (to be able to split multiple spaces) +text = "" +for i=1,#arg do text = text..arg[i].." "; end + +-- spit by spaces, store in reverse order +words = {} +for word in string.gmatch(text, "([^%s]+)") do + table.insert(words, 1, word) +end + +io.write(table.concat(words, " "), "\n") |
