Golang : Detect words using using consecutive letters in a given string
Got an interesting question asked by a member of Go Programming Language Facebook group. He is asking how to return words that have consecutive letters in it. I'm assuming that he is referring to words such as Bookkeeper,toot-toot, veneer-room, and wood-deer.
Below is my answer. Hope this helps.
package main
import (
"fmt"
"strings"
)
func main() {
textString := "Bookkeeper and bookkeeping and sweet-toothed have three consecutive sets of double letters.Others are deer-reeve, feed-door, heel-loop, hoof-footed, hoot-toot, keek-keek, Soonnee, toot-toot, veneer-room, and wood-deer."
words := strings.Split(textString, " ")
// there could be more, but let's start with aa ee oo first.
doubleLetters := []string{"aa", "ee", "oo"}
fmt.Println(textString)
for i := 0; i < len(doubleLetters); i++ {
for _, word := range words {
if strings.Contains(word, doubleLetters[i]) {
fmt.Println("["+word+"] using consecutive letters")
}
}
}
}
You can see the output at https://play.golang.org/p/_MyPC7cJlN8
References:
See also : Golang : Extract part of string with regular expression
By Adam Ng(黃武俊)
IF you gain some knowledge or the information here solved your programming problem. Please consider donating to the less fortunate or some charities that you like. Apart from donation, planting trees, volunteering or reducing your carbon footprint will be great too.
Advertisement
Tutorials
+5.5k Golang : Derive cryptographic key from passwords with Argon2
+3.9k PHP : Extract part of a string starting from the middle
+14.7k Golang : How to check if input from os.Args is integer?
+7.8k Golang : HTTP Routing with Goji example
+5.4k Android Studio : Hello World example
+6.5k SSL : How to check if current certificate is sha1 or sha2 from command line
+9.3k Golang : Flip coin example
+22.6k Golang : Daemonizing a simple web server process example
+12k Golang : Date and Time formatting
+18.8k Golang : Count number of digits from given integer value
+3.8k Python : Find out the variable type and determine the type with simple test
+23.3k Golang : Change file read or write permission example