0
0
DSA Goprogramming~30 mins

Count Words with Given Prefix in DSA Go - Build from Scratch

Choose your learning style9 modes available
Count Words with Given Prefix
📖 Scenario: You are building a simple tool to help a bookstore find how many book titles start with a certain prefix. This helps the store quickly find books related to a topic or author.
🎯 Goal: Build a program that counts how many words in a list start with a given prefix.
📋 What You'll Learn
Create a list of words with exact values
Create a string variable for the prefix to search
Use a loop to count how many words start with the prefix
Print the count as the final output
💡 Why This Matters
🌍 Real World
Searching for words or titles starting with a prefix is common in search engines, autocomplete features, and filtering lists.
💼 Career
Understanding how to filter and count items based on conditions is a key skill in programming and data processing jobs.
Progress0 / 4 steps
1
Create the list of words
Create a slice of strings called words with these exact values: "apple", "app", "application", "banana", "apricot"
DSA Go
Hint

Use []string{} to create a slice of strings with the exact words.

2
Create the prefix variable
Create a string variable called prefix and set it to "app"
DSA Go
Hint

Use prefix := "app" to create the prefix variable.

3
Count words starting with the prefix
Create an integer variable called count and set it to 0. Use a for loop with variable _, word to iterate over words. Inside the loop, use strings.HasPrefix(word, prefix) to check if the word starts with the prefix. If yes, increase count by 1.
DSA Go
Hint

Use count := 0 before the loop. Use for _, word := range words to loop. Use if strings.HasPrefix(word, prefix) to check prefix.

4
Print the count
Use fmt.Println(count) to print the number of words starting with the prefix.
DSA Go
Hint

Use fmt.Println(count) to show the result.