在C語言中,可以使用哈希表或者數組來統計單詞出現的次數。
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 定義哈希表的大小
#define HASH_SIZE 100
// 哈希表節點結構
typedef struct Node {
char word[256]; // 單詞
int count; // 出現次數
struct Node* next;
} Node;
// 哈希函數
int hash(char* word) {
int sum = 0;
for (int i = 0; i < strlen(word); i++) {
sum += word[i];
}
return sum % HASH_SIZE;
}
// 向哈希表中插入節點
void insert(Node** hashTable, char* word) {
int index = hash(word);
Node* newNode = (Node*)malloc(sizeof(Node));
strcpy(newNode->word, word);
newNode->count = 1;
newNode->next = NULL;
if (hashTable[index] == NULL) {
hashTable[index] = newNode;
} else {
Node* cur = hashTable[index];
while (cur->next != NULL) {
if (strcmp(cur->word, word) == 0) {
cur->count++;
free(newNode);
return;
}
cur = cur->next;
}
if (strcmp(cur->word, word) == 0) {
cur->count++;
free(newNode);
} else {
cur->next = newNode;
}
}
}
// 統計單詞出現的次數
void wordCount(char* sentence) {
Node* hashTable[HASH_SIZE] = {NULL};
char* word = strtok(sentence, " ");
while (word != NULL) {
insert(hashTable, word);
word = strtok(NULL, " ");
}
for (int i = 0; i < HASH_SIZE; i++) {
Node* cur = hashTable[i];
while (cur != NULL) {
printf("%s: %d\n", cur->word, cur->count);
cur = cur->next;
}
}
}
int main() {
char sentence[] = "this is a test sentence to count word occurrences";
wordCount(sentence);
return 0;
}
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
// 統計單詞出現的次數
void wordCount(char* sentence) {
typedef struct {
char word[256];
int count;
} Word;
int wordCount = 0;
Word* wordArray = (Word*)malloc(sizeof(Word) * wordCount);
char* word = strtok(sentence, " ");
while (word != NULL) {
int exist = 0;
for (int i = 0; i < wordCount; i++) {
if (strcmp(wordArray[i].word, word) == 0) {
wordArray[i].count++;
exist = 1;
break;
}
}
if (!exist) {
wordCount++;
wordArray = (Word*)realloc(wordArray, sizeof(Word) * wordCount);
strcpy(wordArray[wordCount - 1].word, word);
wordArray[wordCount - 1].count = 1;
}
word = strtok(NULL, " ");
}
for (int i = 0; i < wordCount; i++) {
printf("%s: %d\n", wordArray[i].word, wordArray[i].count);
}
free(wordArray);
}
int main() {
char sentence[] = "this is a test sentence to count word occurrences";
wordCount(sentence);
return 0;
}
以上是兩種統計單詞出現次數的方法,分別使用哈希表和數組來實現。可以根據實際情況選擇適合的方法。