Counting number of repeating words in a Golang String Last Updated : 04 May, 2020 Comments Improve Suggest changes Like Article Like Report Given a string, the task is to count the number of words being repeated in that particular string in Golang. Example: Input: s = "She is mother of my mother." Output: She = 1 is = 1 mother = 2 of = 1 my = 1 To count the number of repeating words, first, the string is taken as input and then strings.Fields() function is used to split the string. A function "repetition" is defined to count the number of words getting repeated. Below is the program in Golang to count the number of repeating words in a given string. C // Golang program to count the number of // repeating words in given Golang String package main import ( "fmt" "strings" ) func repetition(st string) map[string]int { // using strings.Field Function input := strings.Fields(st) wc := make(map[string]int) for _, word := range input { _, matched := wc[word] if matched { wc[word] += 1 } else { wc[word] = 1 } } return wc } // Main function func main() { input := "betty bought the butter , the butter was bitter , " + "betty bought more butter to make the bitter butter better " for index, element := range repetition(input) { fmt.Println(index, "=", element) } } Output: the = 3 , = 2 bitter = 2 to = 1 make = 1 better = 1 betty = 2 bought = 2 butter = 4 was = 1 more = 1 Explanation: In the above program, we first take a string as an input and then split that string using strings.Fields() function. If the same word has occurred, the count increases by one else value 1 is returned that implies that word occurs only once in the string. Comment More infoAdvertise with us Next Article Counting number of repeating words in a Golang String P preetikagupta8171 Follow Improve Article Tags : Go Language Golang-String Golang-Program Similar Reads Repeating a String for Specific Number of Times in Golang In Go language, strings are different from other languages like Java, C++, Python, etc. It is a sequence of variable-width characters where each and every character is represented by one or more bytes using UTF-8 Encoding. You are allowed to repeat a string of for a specific number of times with the 3 min read How to count the number of sentences in a text in R A fundamental task in R that is frequently used in text analysis and natural language processing is counting the number of sentences in a text. Sentence counting is necessary for many applications, including language modelling, sentiment analysis, and text summarization. In this article, we'll look 4 min read strings.Replace() Function in Golang With Examples strings.Replace() Function in Golang is used to return a copy of the given string with the first n non-overlapping instances of old replaced by new one. Syntax: func Replace(s, old, new string, n int) string Here, s is the original or given string, old is the string that you want to replace. new is 2 min read strings.IndexAny() Function in Golang With Examples strings.IndexAny() Function in Golang is used to returns the index of the first instance of any Unicode code point from chars in the original string. If the Unicode code point from chars is not available in the original string, then this method will return -1. Syntax: func IndexAny(str, charstr stri 2 min read strings.SplitN() Function in Golang with Examples strings.SplitN() Function() is a string manipulation function in Go language. It is used to split a given string into substrings separated by a separator. This function returns the slices of all substrings between those separators. Syntax: func SplitN(s, sep string, n int) []string Here, s is the st 3 min read Like