aboutsummaryrefslogtreecommitdiff
path: root/challenge-096/paulo-custodio/basic/ch-1.bas
blob: 5000abf57cffafacd7896ec0b703a04ed33b20e2 (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
' 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"

function join_args() as string
    dim text as string
    dim i as integer

    i = 1
    do while command(i)<>""
        text &= trim(command(i))+" "
        i += 1
    loop
    join_args = trim(text)
end function

sub split_text(words() as string, text as string)
    dim p as integer

    redim preserve words(0)
    text = trim(text)
    p = instr(text, " ")
    do while p > 0
        redim preserve words(ubound(words)+1)
        words(ubound(words)) = left(text, p-1)
        text = trim(mid(text, p+1))
        p = instr(text, " ")
    loop
    'store last segment
    redim preserve words(ubound(words)+1)
    words(ubound(words)) = text
end sub

sub print_reverse(words() as string)
    dim i as integer

    for i = ubound(words) to 1 step -1
        print words(i);" ";
    next i
    print
end sub

dim words() as string
split_text words(), join_args()
print_reverse words()