c - 如何计算 .txt 文件中有多少个单词?在 C

标签 c lexicographic

这是我第一次发布问题。 所以我正在做一个家庭作业计划,并且在一些事情上有点卡住了,我希望有人能插嘴帮助我。这是我需要在程序中做的事情:

  • 您的程序必须读入一个包含带标点符号的句子的文件。
  • 它将句子解析为单词和标点符号。
  • 单词将被输入字典,标点符号将被输入列表。向字典中添加单词时忽略大小写。请记住字典是按字典顺序保存的。
  • 字典和列表中的每个条目都会统计该单词或标点符号在原文中出现的次数。
  • 阅读文本后(第一个字符为 $ 的行终止文本),打印出字典并列出计数。
  • 您的程序接下来将读取格式如下的一行:word1
  • 这意味着将文本中的word1替换为word2

我已经能够输入文件 (hw5-input) 并按字典顺序打印它并去掉了大写字母,我什至有字数统计但无法在单独的行上打印字数统计。 .我还需要交换单词并再次打印文件,但打印字数是我真正需要帮助的地方。这是我目前所拥有的:

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>

#define PUNCT " \n,\t!:;.-"
#define MAX_STR_LEN 2048

struct listNode 
{
    char *word;
    struct listNode *next;
    int wordCount;
};

struct listNode *newListNode(const char * const);
void insertWord(struct listNode *,const char * const);
void deleteList(struct listNode *);
void printList(struct listNode *);

// Create new struct listNode

struct listNode *newListNode(const char * const s) 
{
    struct listNode *n =
        (struct listNode*)calloc(1,sizeof(struct listNode));
    n->word = (char *)calloc(strlen(s)+1,sizeof(*s));
    strcpy(n->word,s);
    n->next = NULL;
    n->wordCount = 1;
    return n;
}

// Insert words into dictionary in ascending order

void insertWord(struct listNode *head,const char * const s) 
{
    char *i;
    int x = 0;
    for(i = s; *i != '\0'; i++) {
        *i = (char)tolower(*i);
        x++;
    }

    i = i-x;

// Gets rid of duplicate words and counts words

    struct listNode *p = head,
        *q = newListNode(i);

    while ((p->next != NULL) && (strcmp(i,p->next->word) > 0)) 
    {
        p = p->next;
    }
    if(p->next != NULL && strcmp(i,p->next->word) == 0)
    {
        p->next->wordCount++;
    } else {
        q->next = p->next;
        p->next = q;
    }
}

// Free all memory allocated for the list

void deleteList(struct listNode *head) 
{
    struct listNode *p = head, *q;
    while (p != NULL) 
    {
        q = p->next;
        free(p->word);
        free(p);
        p = q;
    }
}

// Print the dictionary

void printList(struct listNode *head) 
{
    struct listNode *p = head->next;

    while (p != NULL) 
    {
        printf("%s ",p->word);
        p = p->next;
    }
    puts("");
}

// Enter file and print words in lexicographic order

int main(int argc, char *argv[]) 
{
    char line[MAX_STR_LEN], *s, fileName[MAX_STR_LEN];
    struct listNode *head = newListNode("");

    int i = 0;
    char c;

    FILE *p;

    printf("Enter file name: ");
    scanf("%s", fileName);
        if((p = fopen(fileName, "r")) == NULL)
        {
            printf("File not found.");
            return 0;
        }

    while((c = getc(p)) != '$')
    { 
        line[i] = c;
        i++;
    }

    line[i] = '\0';
    for(s = strtok(line,PUNCT); s != NULL; s = strtok(NULL,PUNCT)) 
    {
        insertWord(head,s);
    }
    printf("Lexicographical order: ");
    printList(head);
    deleteList(head);

    return 0;
}

输入文件(hw5-input)是:

Call me Ishmael.  Some years ago--never mind how long precisely--
having little or no money in my purse, and nothing particular
to interest me on shore, I thought I would sail about a little
and see the watery part of the world.  It is a way I have
of driving off the spleen and regulating the circulation.
Whenever I find myself growing grim about the mouth;
whenever it is a damp, drizzly November in my soul; whenever I
find myself involuntarily pausing before coffin warehouses,
and bringing up the rear of every funeral I meet;
and especially whenever my hypos get such an upper hand of me,
that it requires a strong moral principle to prevent me from
deliberately stepping into the street, and methodically knocking
people's hats off--then, I account it high time to get to sea
as soon as I can.  This is my substitute for pistol and ball.
With a philosophical flourish Cato throws himself upon his sword;
I quietly take to the ship.  There is nothing surprising in this.
If they but knew it, almost all men in their degree, some time
or other, cherish very nearly the same feelings towards
the ocean with me.
$
substitute < replacement
whale < zebra
myself < oneself

我需要它在下一行按字母顺序打印:

a - 4
about - 1
account - 1
ago- 2
and - 5
etc..

最佳答案

http://www.cplusplus.com/reference/clibrary/cstring/strtok/

经过进一步审查,您似乎已经在执行 strtok。您真正的问题是,“字符串的最佳 C 列表实现是什么?”

关于c - 如何计算 .txt 文件中有多少个单词?在 C,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11585849/

相关文章:

string - 字典排序 O(m)

c - 将过剩绘制另存为 PNG/BMP/JPG

C生成最大长度的随机字符串

c - a++ vs a = a + 1 这对高效的内存编程很有用,怎么样?

python - Python中按字典顺序对列表列表进行排序

java - Java 中的字典顺序

c - 按字典顺序对整数数组进行排序

c - 使用结构读取二进制文件以查找记录

c - 数据类型转换(unsigned long long to char)

C++:使用运算符比较字符串的词典顺序