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
+15.8k Golang : Read large file with bufio.Scanner cause token too long error
+4.9k Golang : Get a list of crosses(instruments) available to trade from Oanda account
+8.7k Golang : Gorilla web tool kit schema example
+5.8k AWS S3 : Prevent Hotlinking policy
+12.6k Swift : Convert (cast) Int or int32 value to CGFloat
+9.2k Golang : How to get garbage collection data?
+9.9k Golang : Setting variable value with ldflags
+21.7k Golang : Use TLS version 1.2 and enforce server security configuration over client
+5.5k PHP : Convert string to timestamp or datestamp before storing to database(MariaDB/MySQL)
+13.5k Golang : Activate web camera and broadcast out base64 encoded images
+4.9k Golang : PGX CopyFrom to insert rows into Postgres database
+18.3k Golang : Example for RSA package functions