在C语言编程中,单词计数是一个常见的任务,它可以用于文本分析、词频统计等多个场景。掌握有效的单词计数技巧不仅能够提高代码效率,还能提升编程技能。本文将详细介绍如何在C语言中实现单词计数,并分享一些优化...
在C语言编程中,单词计数是一个常见的任务,它可以用于文本分析、词频统计等多个场景。掌握有效的单词计数技巧不仅能够提高代码效率,还能提升编程技能。本文将详细介绍如何在C语言中实现单词计数,并分享一些优化技巧。
在C语言中,单词计数通常指的是统计一段文本中不同单词的出现次数。一个有效的单词计数程序需要处理以下问题:
以下是一个简单的C语言程序,用于统计文本中单词的数量:
#include
#include
#include
#define MAX_WORD_LENGTH 100
int main() { char text[1000]; char word[MAX_WORD_LENGTH]; int word_count = 0; int index = 0; printf("Enter text: "); fgets(text, sizeof(text), stdin); while (text[index]) { if (isalpha(text[index])) { int i = 0; while (isalpha(text[index])) { word[i++] = tolower(text[index++]); } word[i] = '\0'; // 统计单词出现的次数 word_count++; } else { index++; } } printf("Total number of words: %d\n", word_count); return 0;
} 这个程序首先定义了一个足够大的字符数组来存储输入的文本。然后,它通过遍历文本中的每个字符,使用isalpha函数来判断是否为字母。如果是字母,则将其添加到word数组中,直到遇到非字母字符。这样,我们就得到了一个单词。每次遇到一个单词,我们就增加word_count的值。
对于大型文本,直接统计单词数量可能会很慢。为了优化性能,可以使用哈希表来存储单词和它们的计数。以下是一个使用哈希表的单词计数示例:
#include
#include
#include
#include
#define HASH_TABLE_SIZE 1000
typedef struct WordNode { char *word; int count; struct WordNode *next;
} WordNode;
WordNode *hashTable[HASH_TABLE_SIZE];
unsigned int hash(const char *word) { unsigned int hashValue = 0; while (*word) { hashValue = hashValue * 31 + tolower(*word++); } return hashValue % HASH_TABLE_SIZE;
}
WordNode *createWordNode(const char *word) { WordNode *node = (WordNode *)malloc(sizeof(WordNode)); node->word = strdup(word); node->count = 1; node->next = NULL; return node;
}
void addWord(const char *word) { unsigned int index = hash(word); WordNode *node = hashTable[index]; while (node) { if (strcmp(node->word, word) == 0) { node->count++; return; } node = node->next; } WordNode *newNode = createWordNode(word); newNode->next = hashTable[index]; hashTable[index] = newNode;
}
void printWordCounts() { for (int i = 0; i < HASH_TABLE_SIZE; i++) { WordNode *node = hashTable[i]; while (node) { printf("%s: %d\n", node->word, node->count); node = node->next; } }
}
int main() { char text[1000]; char word[MAX_WORD_LENGTH]; int index = 0; printf("Enter text: "); fgets(text, sizeof(text), stdin); while (text[index]) { if (isalpha(text[index])) { int i = 0; while (isalpha(text[index])) { word[i++] = tolower(text[index++]); } word[i] = '\0'; addWord(word); } else { index++; } } printWordCounts(); return 0;
} 在这个例子中,我们使用了一个简单的哈希表来存储单词和它们的计数。hash函数用于计算单词的哈希值,addWord函数用于将单词添加到哈希表中。最后,printWordCounts函数用于打印所有单词及其计数。
在上述代码中,我们使用了一个固定大小的字符数组来存储单词。在实际应用中,单词的长度可能会有很大的变化。为了解决这个问题,我们可以使用动态字符串来存储单词。
#include
#include
#include
#include
#define HASH_TABLE_SIZE 1000
typedef struct WordNode { char *word; int count; struct WordNode *next;
} WordNode;
WordNode *hashTable[HASH_TABLE_SIZE];
unsigned int hash(const char *word) { unsigned int hashValue = 0; while (*word) { hashValue = hashValue * 31 + tolower(*word++); } return hashValue % HASH_TABLE_SIZE;
}
char *createWord(const char *word) { int length = strlen(word) + 1; char *newWord = (char *)malloc(length); strcpy(newWord, word); return newWord;
}
void addWord(const char *word) { unsigned int index = hash(word); WordNode *node = hashTable[index]; while (node) { if (strcmp(node->word, word) == 0) { node->count++; return; } node = node->next; } WordNode *newNode = (WordNode *)malloc(sizeof(WordNode)); newNode->word = createWord(word); newNode->count = 1; newNode->next = hashTable[index]; hashTable[index] = newNode;
}
void freeHashTable() { for (int i = 0; i < HASH_TABLE_SIZE; i++) { WordNode *node = hashTable[i]; while (node) { WordNode *temp = node; node = node->next; free(temp->word); free(temp); } }
}
int main() { char text[1000]; char word[MAX_WORD_LENGTH]; int index = 0; printf("Enter text: "); fgets(text, sizeof(text), stdin); while (text[index]) { if (isalpha(text[index])) { int i = 0; while (isalpha(text[index])) { word[i++] = tolower(text[index++]); } word[i] = '\0'; addWord(word); } else { index++; } } printWordCounts(); freeHashTable(); return 0;
} 在这个版本中,我们使用createWord函数来动态分配内存并创建新的单词。在freeHashTable函数中,我们释放了哈希表中所有单词的内存。
通过以上示例,我们可以看到如何在C语言中实现单词计数,并使用一些优化技巧来提高代码效率。掌握这些技巧将有助于你在C语言编程中解决更复杂的文本处理问题。