Untitled

mail@pastecode.io avatar
unknown
python
a year ago
1.3 kB
4
Indexable
Never
def first_word(sentence):
    i = 0
    first_word = ""
    while True:
        if sentence[i] == " ":
            break
        first_word = first_word + sentence[i]
        i += 1
    return first_word

def second_word(sentence):
    second_word = ""
    first_space = sentence.find(" ")
    if first_space == -1:
        return
    sentence = sentence[first_space + 1:] #replace the sentence with new one without the first word and the first space
    i = 0
    while i < len(sentence):
        if sentence[i] == " ":
            break
        second_word = second_word + sentence[i]
        i += 1
    return second_word

def last_word(sentence):
    index = len(sentence) - 1
    last_word = ""
    flip_word = ""
    while True:
        if sentence[index] == " ":
            break
        last_word = last_word + sentence[index]
        index -= 1
    j = 0
    k = len(last_word) - 1
    while j <= len(last_word) - 1:
        flip_word = flip_word + last_word[k]
        j += 1
        k -= 1
    last_word = flip_word

    return last_word


# You can test your function by calling it within the following block
if __name__ == "__main__":
    sentence = "first second"
    print(first_word(sentence))
    print(second_word(sentence))
    print(last_word(sentence))