我要数一数这个小文字“心碎的心碎”每一个字出现的次数。
这段文字的每个单词都是 2darray[100][20]
的,其中 100 是 the max_words
,20 是 the max_word_length
。我有一个指针array[100]
,其中每个指针都指向这个词。例如,我找不到计算相同单词的聪明方法
a: 2 times
broken: 2 times
heart: 1 time
mind: 1 time
. : 1 time
这些将是指针和单词数组:
POINTERS ARRAY WORDS ARRAY
point0(points "a") a
point1(points "broken") broken
point2(points "heart") heart
point3(points "of") of
point4 (points "a") mind
point5(points "broken") .
point6(points "mind") \0\0\0\0\0
point7(points ".") \0\0\0\0\0
NULL ..
NULL
..
NULL \0\0\0\0\0
旁注:每个单词都是小写的。
void frequence_word(char *pointers[], int frequence_array[]) {
int word = 0;
int i;
int count = 1;
int check[MAX_WORDS];
for (word = 0; word < MAX_WORDS; word++) {
check[word] = -1;
}
for (word = 0; word < MAX_WORDS; word++) {
count = 1;
for (i = word + 1; i < MAX_WORDS; i++) {
if (pointers[word + 1] != NULL
&& strcmp(pointers[word], pointers[i]) == 0) {
count++;
check[i] = 0;
}
}
if (check[word] != 0) {
check[word] = count;
}
}
}
请问有什么想法吗?