-
-
Notifications
You must be signed in to change notification settings - Fork 564
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
1473172
commit 751f110
Showing
1 changed file
with
31 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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])) | ||
} | ||
|
||
} |