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语言数组不完全赋值的原因有哪些

    原因一:程序员没有为数组的所有元素赋值。这可能是由于疏忽或者考虑到某些元素不需要赋特定的值而忽略了它们。 原因二:程序逻辑错误。在设计程序时,可能没有考...

  • c语言怎么持续输入输出

    在C语言中,可以使用循环结构来实现持续的输入和输出。
    要持续输入,可以使用while循环或do-while循环结构,并在循环体内使用scanf函数接收输入。
    示例...

  • c语言如何控制输出速度

    在C语言中,可以使用sleep()函数来控制输出的速度。sleep()函数可以让程序暂停执行一定的时间,单位是秒。
    以下是一个示例代码,每输出一个字符后暂停1秒钟...

  • c语言定时器如何定时两秒

    在C语言中,可以使用sleep()函数来实现定时器功能。
    #include #include int main() { printf("Start\n"); sleep(2); // 休眠2秒 printf("End\n"); return 0...

  • 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中字符串的一个方法,用于去除字符串的首尾空白字符(包括空格、制表符、换行符等)。它的作用是将字符串两端的空白字符删除,并返回删除后的...