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
+14.3k Golang : Get timezone offset from date or timestamp
+5.4k Golang & Javascript : How to save cropped image to file on server
+16.5k Golang : Clone with pointer and modify value
+18.7k Golang : Archive directory with tar and gzip
+6.2k Golang : Get Alexa ranking data example
+18.1k Golang : Clearing slice
+5.8k Golang : Calculate diameter, circumference, area, sphere surface and volume
+31.2k Golang : Convert []string to []byte examples
+10.8k Golang : Convert(cast) float to int
+9.6k Android Studio : Checkbox for user to select options example
+18.9k Golang : How to get time from unix nano example
+9.3k Golang : Read file and convert content to string