Complete the code to return the length of the prefix string.
int prefixLength(char *prefix) {
return [1];
}The strlen function returns the length of a string in C.
Complete the code to compare the prefix with the start of the word.
int startsWith(char *word, char *prefix) {
int len = strlen(prefix);
return strncmp(word, prefix, [1]) == 0;
}We compare only the first len characters to check if word starts with prefix.
Fix the error in the loop that counts words starting with the prefix.
int countWordsWithPrefix(char **words, int wordsSize, char *prefix) {
int count = 0;
for (int i = 0; i < [1]; i++) {
if (startsWith(words[i], prefix)) {
count++;
}
}
return count;
}The loop should run from 0 to wordsSize to cover all words.
Fill both blanks to create a function that counts words starting with the prefix.
int countWordsWithPrefix(char **words, int wordsSize, char *prefix) {
int count = 0;
for (int [1] = 0; [2] < wordsSize; [2]++) {
if (startsWith(words[i], prefix)) {
count++;
}
}
return count;
}The loop variable should be consistently named i in initialization and condition.
Fill all three blanks to implement the full function counting words with the prefix.
int countWordsWithPrefix(char **words, int wordsSize, char *prefix) {
int count = 0;
int prefixLen = [1](prefix);
for (int i = 0; i < [2]; i++) {
if (strncmp(words[i], prefix, [3]) == 0) {
count++;
}
}
return count;
}We get the prefix length with strlen, loop over wordsSize, and compare first prefixLen characters.
