Skip to content

Commit

Permalink
Add Longest Word in Kotlin (#3997)
Browse files Browse the repository at this point in the history
  • Loading branch information
mechance782 authored Nov 11, 2024
1 parent 1473172 commit 751f110
Showing 1 changed file with 31 additions and 0 deletions.
31 changes: 31 additions & 0 deletions archive/k/kotlin/LongestWord.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,31 @@
fun main(args: Array<String>) {

fun longestWord(sentence: String): Any {
// if sentence is empty, ask for a String
if (sentence.length == 0){
return "Usage: please provide a string"
} else {
// split sentence from these delimeters and put resulting strings into words list
var words = sentence.split(" ", "\t", "\n", "\r")
var longest = 0
// iterate through words list and compare each word length to longest var
// if word length is larger, then the var longest will be assigned the word length
for (word in words){
when {
word.length > longest -> longest = word.length
}
}
// return var longest which holds the largest string length in the sentence parameter
return longest
}
}

// if console input is null, ask for String
if (args.isNullOrEmpty()){
println("Usage: please provide a string")
} else {
// if console input is not null, then find longestWord of input String
println(longestWord(args[0]))
}

}

0 comments on commit 751f110

Please sign in to comment.