jweinst1
9/14/2015 - 7:18 AM

some basic string operations in ruby

some basic string operations in ruby

#returns a string of integers
def numstr(x)
    s = ""
    for num in 0..x
        s << num.to_s
    end
    return s
end

#returns a list of words from a string
def wordlist(x)
    return x.split(" ")
end

#returns a list of chars from the specificed string
def filterchar(x, elem)
    chars = []
    for char in x.split("")
        if char == elem
            chars << char
        end
    end
    return chars
end
#returns an array of indexes of a letter in a string
def indexstr(x, elem)
    list = x.split("")
    indices = []
    count = 0
    until count == list.length do
        if list[count] == elem
            indices << count
            count += 1
        else
            count += 1
        end
    end
    return indices
end
#reverses a string one at a time.
def reversestr(x)
    newstr = ""
    list = x.split("")
    count = list.length-1
    until count == -1 do
        newstr << list[count]
        count -= 1
    end
    return newstr
end