#include
#include
int prefixCount(char**words, int wordsSize, char* pref) {
int res = 0;
int len = strlen(pref);
for (int i = 0; i < wordsSize; i++) {
if (strncmp(words[i], pref,len) == 0) {
res++;
}
}
return res;
}
int main()
{
char words[][10] = { "pay", "attention", "practice", "attend" };
char pref[] = "code";
printf("%d", prefixCount(words,4,pref));
}
#include<stdio.h>
#include<string.h>
int prefixCount(char**words, int wordsSize, char* pref) {
int res = 0;
int len = strlen(pref);
for (int i = 0; i < wordsSize; i++) {
if (strncmp(words[i], pref,len) == 0) {
res++;
}
}
return res;
}
int main()
{
char *words[][10] = { "pay", "attention", "practice", "attend" };
char pref[] = "at";
char** p = (char**)words;
printf("%d", prefixCount(p,4,pref));
}
这样写即可,望采纳!
#include<stdio.h>
#include<string.h>
int prefixCount(char** words, int wordsSize, char* pref)
{
int res = 0;
int len = strlen(pref);
for (int i = 0; i < wordsSize; i++)
{
if (strncmp(words[i], pref, len) == 0)
{
res++;
}
}
return res;
}
int main()
{
char* words[] = { "pay", "attention", "practice", "attend" };
char* pref = "code";
printf("Number of strings with prefix '%s': %d\n", pref, prefixCount(words, 4, pref));
return 0;
}
帮你改了代码