📖 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 know how many books belong to a specific category or author initials.
🎯 Goal: Create a program that counts how many words in a list start with a given prefix.
📋 What You'll Learn
Create an array of strings called words with exact values
Create a string variable called prefix with the exact value
Write a function countPrefix that counts how many words start with prefix
Print the count using printf
💡 Why This Matters
🌍 Real World
Counting words with a given prefix is useful in search engines, autocomplete features, and filtering lists quickly.
💼 Career
Understanding string handling and prefix matching is important for software developers working on text processing, search algorithms, and user interface features.
Progress0 / 4 steps
1
Create the list of words
Create an array of strings called words with these exact values: "apple", "app", "application", "banana", "apricot".
DSA C
Hint
Use an array of pointers to strings in C to store the words.
2
Create the prefix variable
Create a string variable called prefix and set it to the exact value "app".
DSA C
Hint
Use a character array to store the prefix string.
3
Write the function to count words with the prefix
Write a function called countPrefix that takes words, the number of words (5), and prefix as parameters. It should return the count of words starting with prefix. Use strncmp from string.h to compare prefixes.
DSA C
Hint
Use a loop to check each word. Use strncmp to compare the start of each word with the prefix.
4
Print the count of words with the prefix
Call the function countPrefix with words, 5, and prefix. Print the returned count using printf with the exact format: "Count: %d\n".
DSA C
Hint
Call countPrefix with the correct arguments and print the result exactly as shown.
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 given prefix
B. Count the total number of characters in all words
C. Find the longest word in the list
D. Sort the words alphabetically
Solution
Step 1: Understand the problem statement
The problem asks to find how many words start with a specific prefix string.
Step 2: Identify the key operation
We need to check each word and count those that begin with the given prefix.
Final Answer:
Count how many words in a list start with a given prefix -> Option A
Quick Check:
Count words starting with prefix = Count how many words in a list start with a given prefix [OK]
Hint: Look for words starting exactly with the prefix [OK]
Common Mistakes:
Counting total characters instead of words
Looking for words containing prefix anywhere
Sorting words instead of counting
2. Which of the following C code snippets correctly checks if a word starts with a given prefix prefix?
easy
A. strcmp(word, prefix) == 0
B. strncmp(word, prefix, strlen(prefix)) == 0
C. strstr(word, prefix) != NULL
D. strlen(word) == strlen(prefix)
Solution
Step 1: Understand string comparison functions
strncmp compares first n characters; here n = length of prefix.
Step 2: Check which code confirms prefix match
strncmp(word, prefix, strlen(prefix)) == 0 means word starts with prefix exactly.
Final Answer:
strncmp(word, prefix, strlen(prefix)) == 0 -> Option B
Quick Check:
strncmp with prefix length = 0 means prefix match [OK]
Hint: Use strncmp with prefix length to check start [OK]
Common Mistakes:
Using strcmp which requires full string match
Using strstr without checking start position
Comparing lengths only
3. What is the output of the following C code?
char *words[] = {"apple", "app", "application", "banana"};
char prefix[] = "app";
int count = 0;
for (int i = 0; i < 4; i++) {
if (strncmp(words[i], prefix, strlen(prefix)) == 0) {
count++;
}
}
printf("%d\n", count);
Hint: Count words where strncmp returns 0 for prefix length [OK]
Common Mistakes:
Counting words that contain prefix anywhere
Off-by-one errors in loop
Incorrect prefix length in strncmp
4. Find the bug in this C code that counts words starting with prefix prefix:
int countWords(char *words[], int n, char *prefix) {
int count = 0;
for (int i = 0; i <= n; i++) {
if (strncmp(words[i], prefix, strlen(prefix)) == 0) {
count++;
}
}
return count;
}
medium
A. Initialize count to 1 instead of 0
B. Use strcmp instead of strncmp
C. Loop condition should be i < n, not i <= n
D. strlen(prefix) should be replaced with strlen(words[i])
Solution
Step 1: Analyze loop boundary
Loop runs from i = 0 to i <= n, which accesses words[n] (out of bounds).
Step 2: Correct loop condition
Change to i < n to avoid accessing beyond array size.
Final Answer:
Loop condition should be i < n, not i <= n -> Option C
Quick Check:
Array index must be less than size [OK]
Hint: Use i < n for array loops to avoid overflow [OK]
Common Mistakes:
Using <= instead of < in loops
Confusing strcmp and strncmp usage
Wrong length argument in strncmp
5. You have a list of words and want to count how many start with prefix pre. Which approach is best to optimize counting if you must do many prefix queries efficiently?
hard
A. Build a trie (prefix tree) from words and count nodes matching prefix
B. Use strncmp on each word for every query
C. Sort words and use binary search for prefix range
D. Store words in a hash set and check prefix membership
Solution
Step 1: Understand multiple prefix queries
Checking each word with strncmp repeatedly is slow for many queries.
Step 2: Choose data structure for fast prefix counting
Trie stores words by prefixes, allowing quick count of words with given prefix.
Step 3: Compare alternatives
Binary search helps but trie is more direct for prefix counts; hash set doesn't support prefix queries well.
Final Answer:
Build a trie (prefix tree) from words and count nodes matching prefix -> Option A
Quick Check:
Trie = fast prefix count [OK]
Hint: Use trie for fast multiple prefix queries [OK]
Common Mistakes:
Using linear search for many queries
Ignoring trie benefits for prefix problems
Using hash set which doesn't support prefix search
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.