-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathContents.swift
41 lines (30 loc) · 1013 Bytes
/
Contents.swift
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
import UIKit
var sampleSentence = "Let starts today by completing a very interesting challenge"
func reverseWordsInSentence(sentence: String) -> String{
let allWords = sentence.components(separatedBy: " ")
var newSentence = ""
for index in 0...allWords.count - 1{
let word = allWords[index]
if newSentence != ""{
newSentence += " "
}
if index % 2 == 1{
let reverseWord = String(word.reversed())
newSentence += reverseWord.stringByRemovingVowels()
} else{
newSentence += word.stringByRemovingVowels()
}
}
return newSentence
}
extension String{
func stringByRemovingVowels() -> String{
var newWord = self
for vowel in ["a", "e", "i", "o", "u"]{
newWord = newWord.replacingOccurrences(of: vowel, with: "")
}
return newWord
}
}
// MARK: - PLAYGROUND
print(reverseWordsInSentence(sentence: sampleSentence))