c語言怎么統(tǒng)計(jì)單詞出現(xiàn)的次數(shù)

小億
216
2023-11-21 03:29:06
欄目: 編程語言

在C語言中,可以使用哈希表或者數(shù)組來統(tǒng)計(jì)單詞出現(xiàn)的次數(shù)。

  1. 使用哈希表:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 定義哈希表的大小
#define HASH_SIZE 100

// 哈希表節(jié)點(diǎn)結(jié)構(gòu)
typedef struct Node {
    char word[256];  // 單詞
    int count;       // 出現(xiàn)次數(shù)
    struct Node* next;
} Node;

// 哈希函數(shù)
int hash(char* word) {
    int sum = 0;
    for (int i = 0; i < strlen(word); i++) {
        sum += word[i];
    }
    return sum % HASH_SIZE;
}

// 向哈希表中插入節(jié)點(diǎn)
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;
        }
    }
}

// 統(tǒng)計(jì)單詞出現(xiàn)的次數(shù)
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;
}
  1. 使用數(shù)組:
#include <stdio.h>
#include <stdlib.h>
#include <string.h>

// 統(tǒng)計(jì)單詞出現(xiàn)的次數(shù)
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;
}

以上是兩種統(tǒng)計(jì)單詞出現(xiàn)次數(shù)的方法,分別使用哈希表和數(shù)組來實(shí)現(xiàn)??梢愿鶕?jù)實(shí)際情況選擇適合的方法。

0