📖 Scenario: You are working on a simple text search feature. You have a list of words from a document, and you want to find out how many words start with a certain prefix typed by a user.
🎯 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
Use a loop to count words starting with the prefix
Print the count of matching words
💡 Why This Matters
🌍 Real World
Counting words with a given prefix is useful in search features, autocomplete suggestions, and filtering lists in apps.
💼 Career
This skill helps in building efficient text search and filtering functions in software development roles.
Progress0 / 4 steps
1
Create the list of words
Create a List<string> called words with these exact entries: "apple", "application", "banana", "apricot", "berry".
DSA C#
Hint
Use new List<string> { ... } to create the list with the exact words.
2
Create the prefix variable
Create a string variable called prefix and set it to "app".
DSA C#
Hint
Use string prefix = "app"; to create the prefix variable.
3
Count words starting with the prefix
Create an int variable called count and set it to 0. Use a foreach loop with variable word to go through words. Inside the loop, use word.StartsWith(prefix) to check if the word starts with the prefix. If yes, increase count by 1.
DSA C#
Hint
Use foreach (string word in words) and inside it check if (word.StartsWith(prefix)).
4
Print the count
Use Console.WriteLine to print the value of count.
DSA C#
Hint
Use Console.WriteLine(count); to show the result.
Practice
(1/5)
1. What does the problem "Count Words with Given Prefix" ask you to do?
easy
A. Count how many words in a list start with a specific prefix
B. Find the longest word in a list
C. Sort words alphabetically
D. Replace the prefix in all words with another string
Solution
Step 1: Understand the problem statement
The problem asks to count words that begin with a given prefix, not to sort or replace words.
Step 2: Identify the key action
Counting words starting with the prefix is the main task.
Final Answer:
Count how many words in a list start with a specific prefix -> Option A
Quick Check:
Count words with prefix = Count how many words in a list start with a specific prefix [OK]
Hint: Focus on words starting with prefix, not other operations [OK]
Common Mistakes:
Confusing counting with sorting
Thinking the task is to replace prefixes
Ignoring the prefix condition
2. Which of the following C# code snippets correctly checks if a string word starts with a prefix prefix?
easy
A. if (word == prefix) { /* do something */ }
B. if (word.Contains(prefix)) { /* do something */ }
C. if (word.EndsWith(prefix)) { /* do something */ }
D. if (word.StartsWith(prefix)) { /* do something */ }
Solution
Step 1: Recall string methods in C#
The method StartsWith checks if a string begins with a given substring.
Step 2: Compare options
Contains checks anywhere in the string, EndsWith checks the end, and == checks exact equality.
Final Answer:
if (word.StartsWith(prefix)) { /* do something */ } -> Option D
Quick Check:
Use StartsWith to check prefix [OK]
Hint: Use StartsWith method to check prefix in C# strings [OK]
Common Mistakes:
Using Contains instead of StartsWith
Checking equality instead of prefix
Using EndsWith which checks suffix
3. What is the output of the following C# code?
string[] words = {"apple", "app", "application", "banana"};
string prefix = "app";
int count = 0;
foreach (var word in words) {
if (word.StartsWith(prefix)) {
count++;
}
}
Console.WriteLine(count);
Hint: Count words starting with prefix by checking each word [OK]
Common Mistakes:
Counting only exact matches
Including words that contain prefix but don't start with it
Off-by-one counting errors
4. The following code is intended to count words starting with a prefix but gives wrong output. What is the error?
string[] words = {"cat", "car", "dog", "cart"};
string prefix = "car";
int count = 0;
for (int i = 0; i <= words.Length; i++) {
if (words[i].StartsWith(prefix)) {
count++;
}
}
Console.WriteLine(count);
medium
A. The loop condition should be i < words.Length instead of <=.
B. The prefix variable is incorrect.
C. The StartsWith method is used wrongly.
D. The count variable should be initialized inside the loop.
Solution
Step 1: Analyze the for loop condition
The loop runs from i = 0 to i <= words.Length, which causes an index out of range error because valid indices are 0 to words.Length - 1.
Step 2: Correct the loop condition
Change condition to i < words.Length to avoid accessing invalid index.
Final Answer:
The loop condition should be i < words.Length instead of <=. -> Option A
Quick Check:
Loop index must be less than array length [OK]
Hint: Use i < array.Length to avoid index errors in loops [OK]
Common Mistakes:
Using <= instead of < in loop condition
Reinitializing count inside loop
Misusing StartsWith method
5. Given a list of words ["tree", "trie", "trigger", "trip", "try", "matrix"] and prefix "tri", which C# code snippet correctly counts the words starting with the prefix and prints the count?
hard
A. int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count);
B. int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count);
C. int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count);
D. int count = words.Length; Console.WriteLine(count);
Solution
Step 1: Understand the requirement
We need to count words starting exactly with prefix "tri".
Step 2: Evaluate each option
int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); uses LINQ Count with StartsWith correctly. int count = 0; foreach(var word in words) { if(word.Contains(prefix)) count++; } Console.WriteLine(count); uses Contains which counts words containing prefix anywhere. int count = 0; for(int i=0; i<=words.Length; i++) { if(words[i].StartsWith(prefix)) count++; } Console.WriteLine(count); has loop with <= causing index error. int count = words.Length; Console.WriteLine(count); counts all words ignoring prefix.
Final Answer:
int count = words.Count(word => word.StartsWith(prefix)); Console.WriteLine(count); -> Option B
Quick Check:
Use LINQ Count with StartsWith for prefix count [OK]
Hint: Use LINQ Count with StartsWith for concise prefix counting [OK]
Common Mistakes:
Using Contains instead of StartsWith
Loop index out of range errors
Counting all words without prefix check
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.