中文字幕av专区_日韩电影在线播放_精品国产精品久久一区免费式_av在线免费观看网站

溫馨提示×

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
永年县| 太保市| 丰宁| 泸溪县| 会泽县| 彭阳县| 观塘区| 和平区| 张掖市| 鲁甸县| 横峰县| 四子王旗| 芒康县| 泗水县| 桦甸市| 琼结县| 井冈山市| 竹北市| 崇义县| 牟定县| 宝清县| 永川市| 深水埗区| 缙云县| 陆良县| 江孜县| 巴马| 嘉峪关市| 阜平县| 保亭| 吐鲁番市| 玉门市| 南华县| 马山县| 潞西市| 黔西县| 通州市| 葫芦岛市| 尼勒克县| 蒲城县| 福安市|