117.info
人生若只如初见

c语言怎么统计单词出现的次数

在C语言中,可以使用哈希表或者数组来统计单词出现的次数。

  1. 使用哈希表:
#include 
#include 
#include 

// 定义哈希表的大小
#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 
#include 
#include 

// 统计单词出现的次数
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;
}

以上是两种统计单词出现次数的方法,分别使用哈希表和数组来实现。可以根据实际情况选择适合的方法。

未经允许不得转载 » 本文链接:https://www.117.info/ask/fef34AzsLAgFSAlw.html

推荐文章

  • c语言goto用法是什么

    在C语言中,goto语句用于从一个位置跳转到另一个位置。它可以用于跳出循环、条件语句或者函数。goto语句的语法如下:
    goto label;
    ...
    label: //...

  • goto在c语言中的运用方法是什么

    goto 语句在 C 语言中允许程序从一个位置跳转到另一个位置。然而,在实际编程中,过度使用 goto 语句通常被认为是一种不好的编程实践,因为它可能导致代码结构混...

  • c语言堆和栈的区别是什么

    C语言中的堆和栈是两种不同的内存分配方式。 栈(Stack):栈是由编译器自动分配和释放的内存区域,用于存储局部变量和函数调用的上下文信息。栈的特点是先进后出...

  • C语言结构体数组怎么定义和使用

    在C语言中,可以通过定义结构体数组来存储多个具有相同数据类型的结构体对象。下面是定义和使用结构体数组的示例代码:
    #include // 定义一个结构体类型

  • php工作流实现的代码怎么写

    实现PHP工作流的代码可以通过使用流程控制语句、条件判断和函数等来实现。以下是一个简单的示例代码来说明如何实现PHP工作流: 以上代码定义了一个名为workflow的...

  • django工作流引擎怎么使用

    使用Django工作流引擎可以通过以下步骤: 安装django-workflows库:可以通过pip安装,命令为pip install django-workflows。 在Django项目的settings.py文件中,...

  • PHP中strip_tags的用法是什么

    在PHP中,strip_tags函数用于从字符串中删除HTML和PHP标签。它的基本用法如下:
    strip_tags(string $str, array|string|null $allowed_tags = null): strin...

  • python中strip的作用是什么

    strip()函数是Python中字符串的一个方法,用于去除字符串的首尾空白字符(包括空格、制表符、换行符等)。它的作用是将字符串两端的空白字符删除,并返回删除后的...