Count Words with Given Prefix
📖 Scenario: You are building a simple program to help a bookstore find how many book titles start with a certain prefix. This helps the store quickly count books by categories or themes.
🎯 Goal: Create a program that counts how many words in a list start with a given prefix.
📋 What You'll Learn
Create a vector of strings called
words with the exact values: "apple", "app", "application", "banana", "band"Create a string variable called
prefix and set it to "app"Use a
for loop with variable word to iterate over wordsInside the loop, use
word.compare(0, prefix.size(), prefix) == 0 to check if word starts with prefixCreate an integer variable
count to count how many words start with prefixPrint the value of
count at the end💡 Why This Matters
🌍 Real World
Counting words with a specific prefix is useful in search engines, autocomplete features, and organizing data by categories.
💼 Career
This skill helps in software development roles involving text processing, search optimization, and building user-friendly interfaces.
Progress0 / 4 steps