Created
March 29, 2019 15:50
-
-
Save vivan-snapretail/2fde813217946dcf42ef16f236743be3 to your computer and use it in GitHub Desktop.
longest word in go
This file contains hidden or 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
package main | |
import "fmt" | |
func LongestWord(sen string) string { | |
// code goes here | |
// Note: feel free to modify the return type of this function | |
// fmt.Println("Length: ", len(sen)) | |
inWord := false | |
isLetter := false | |
wordStart := 0 | |
longestWordStart := 0 | |
longestWordLength := 0 | |
for i, c := range sen { | |
isLetter = (c >= 65 && c <= 122) || (c >= 48 && c <= 57) | |
if !inWord && isLetter { | |
wordStart = i | |
} | |
inWord = isLetter | |
// fmt.Printf("\n%v: %s, isLetter: %v, inWord: %v", i, string(c), isLetter, inWord) | |
if inWord { | |
wordLength := i - wordStart + 1 | |
if wordLength > longestWordLength { | |
longestWordStart = wordStart | |
longestWordLength = wordLength | |
} | |
} | |
} | |
// fmt.Println() | |
// fmt.Println(longestWordStart, longestWordLength) | |
return string(sen[longestWordStart : longestWordStart+longestWordLength]) | |
} | |
func main() { | |
// do not modify below here, readline is our function | |
// that properly reads in the input for you | |
fmt.Println(LongestWord(readline())) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment