Hello and welcome to today's lesson! We are going to unravel an engaging challenge that will sharpen our skills in string manipulation using Go. This lesson will place particular emphasis on nested loops. Get ready for an intriguing task as we explore how to extract odd-indexed characters from each word in a sentence, but only if the word has an even number of characters. Does that sound exciting? Let's dive in!
The task we'll be demonstrating is as follows: we will work with a string representing a sentence in which words are separated by spaces. Your challenge involves creating a Go function that identifies the odd-indexed characters of words that have an even number of characters and then combines these characters into a single string, maintaining the order in which they appeared in the sentence.
Consider this example: "Go is a powerful language for programming" The word Go
has 2 characters (an even number), and we'll select the odd-indexed character from this word, specifically, o
. The word is
also has 2 characters, and we'll select the odd-indexed character s
. Similarly, we'll select o
, e
, f
, l
from powerful
, a
, g
, a
, and e
from language
. We'll skip the words a
, for
, and programming
because they have odd lengths.
If our function is working correctly, it should return osoeflagae
. Isn't it fascinating to see what we can extract from a simple sentence?
We will commence our solution-building process by splitting the sentence into words. In Go, this can be efficiently achieved using the strings.Fields
function, which splits the string into a slice of words using whitespace characters as delimiters.
Go1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func solution(sentence string) string { 9 words := strings.Fields(sentence) 10 // we will proceed from here 11 return "" 12}
We now introduce nested loops: an outer loop that iterates over every single word, and an inner loop that checks every character within each word. First, we'll use an if condition to verify whether a word has an even length. We can determine this by checking whether len(word) % 2 == 0
. If it does, the word has an even length!
Go1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func solution(sentence string) string { 9 words := strings.Fields(sentence) 10 var result string 11 12 for _, word := range words { 13 if len(word)%2 == 0 { // check if the length of the word is even 14 // we are building up our solution gradually 15 } 16 } 17 return result 18}
With our outer loop set, we're ready to implement our inner loop. We aim to iterate only over the odd-indexed characters in each word of even length. To accomplish this, we'll start from an index of 1 and increment by 2 each time. This method ensures our loop only selects the characters at odd indexes.
We'll then append these characters to a result slice, which we will join into a final string for output.
Go1package main 2 3import ( 4 "fmt" 5 "strings" 6) 7 8func solution(sentence string) string { 9 words := strings.Fields(sentence) 10 var result []rune 11 12 for _, word := range words { 13 if len(word)%2 == 0 { // check if the length of the word is even 14 for i := 1; i < len(word); i += 2 { // loop over odd-indexed characters 15 result = append(result, rune(word[i])) 16 } 17 } 18 } 19 return string(result) 20} 21 22func main() { 23 fmt.Println(solution("Go is a powerful language for programming.")) // Outputs: osoeflagae 24}
Well done! You've successfully navigated the realms of nested loops to extract specific characters from words within a sentence using Go. You've learned how to analyze a sentence by breaking it down into its constituent words and studying each word at a deeper level. Now, use this knowledge as a solid foundation in your further exploration of nested loops and string manipulations in Go. The key to mastering this topic is practice; the more you apply what you've learned, the more concrete your understanding becomes. Are you excited to continue exploring nested loops, string manipulations, and the endless possibilities of Go? Keep diving deeper!