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
+17.8k Golang : Check if a directory exist or not
+16.7k Golang : Fix cannot convert buffer (type *bytes.Buffer) to type string error
+20.9k Golang : Create and resolve(read) symbolic links
+5.8k Golang : Grab news article text and use NLP to get each paragraph's sentences
+26.9k Golang : Find files by name - cross platform example
+16.8k Golang : How to save log messages to file?
+7k Golang : Dealing with postal or zip code example
+8.2k Golang : How to check if input string is a word?
+7.3k Gogland : Single File versus Go Application Run Configurations
+15.9k Golang : Loop each day of the current month example
+14k Golang : Convert IP version 6 address to integer or decimal number
+6.7k Golang : Calculate BMI and risk category