-
Notifications
You must be signed in to change notification settings - Fork 0
/
ex3.kt
43 lines (38 loc) · 1.09 KB
/
ex3.kt
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
/**
* You can edit, run, and share this code.
* play.kotlinlang.org
*/
fun WordCount(phrase: String)
{
var i:Int = 0
val WordCountMap: MutableMap<String, Int>? = mutableMapOf()
while(i < phrase.length - 1)
{
if(phrase[i].isLetterOrDigit())
{
// Flow: Start to detect word
var j = i + 1
while(!phrase[j].equals(0))
if(phrase[j].isLetterOrDigit() or (phrase[j].equals('\'') && phrase[j+1].isLetterOrDigit()))
j++
else
break
var word = phrase.slice(i..(j - 1)).lowercase()
if(WordCountMap?.containsKey(word) ?: false)
{
var numN = WordCountMap?.get(word) ?: 0
numN++
WordCountMap?.set(word, numN)
}
else
WordCountMap?.set(word, 1)
i += (j-i)
}
else
i++
}
println(WordCountMap)
}
fun main() {
WordCount("\"That's the password: 'PASSWORD 123'!\", cried the Special Agent.\nSo I fled.")
}