亚洲激情专区-91九色丨porny丨老师-久久久久久久女国产乱让韩-国产精品午夜小视频观看

溫馨提示×

c語言怎么統計單詞出現的次數

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

在C語言中,可以使用哈希表或者數組來統計單詞出現的次數。

  1. 使用哈希表:
#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;
}
  1. 使用數組:
#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;
}

以上是兩種統計單詞出現次數的方法,分別使用哈希表和數組來實現。可以根據實際情況選擇適合的方法。

0
桑日县| 同德县| 清水县| 明光市| 侯马市| 灵台县| 安阳县| 仪征市| 石棉县| 信丰县| 贵定县| 福建省| 越西县| 平顶山市| 马公市| 兴安盟| 永兴县| 定结县| 凉山| 开化县| 桑植县| 康定县| 阿拉尔市| 尼勒克县| 枣强县| 中西区| 民丰县| 彩票| 都兰县| 永州市| 靖安县| 西吉县| 武功县| 宁波市| 清水河县| 龙里县| 讷河市| 南郑县| 庐江县| 连山| 明水县|