在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;
}
以上是两种统计单词出现次数的方法,分别使用哈希表和数组来实现。可以根据实际情况选择适合的方法。