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
+11.2k Golang : How to determine a prime number?
+36.7k Golang : Display float in 2 decimal points and rounding up or down
+12.8k Swift : Convert (cast) Int or int32 value to CGFloat
+11.1k Golang : Generate random elements without repetition or duplicate
+10k CodeIgniter : Load different view for mobile devices
+36.4k Golang : How to split or chunking a file to smaller pieces?
+14.2k Javascript : Prompt confirmation before exit
+9.2k Golang : Write multiple lines or divide string into multiple lines
+9.3k Golang : Create and shuffle deck of cards example
+8.9k Golang : Accept any number of function arguments with three dots(...)
+9k Golang : Go as a script or running go with shebang/hashbang style
+9.1k Golang : How to capture return values from goroutines?