c语言在文中查找100个单词

输入文本文件的名称应用户的输入来提供。也就是说,输入文件不应该被硬编码到程序的源代码中,但是程序应该向用户询问输入文件。
程序应打印输入文件中的单词总数、输入文件中不同单词的数量,以及最常出现的100个单词及其频率,这些单词按其频率按降序排列。该程序还应测量和打印处理时间。


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

#define HASH_TABLE_SIZE 49999

typedef struct word {
    char wrd[80];
    int num;
}word;
static int diff_words = 0;
static int total_words = 0;

word* hashtable[HASH_TABLE_SIZE];

int hash(int number) {
    return number % HASH_TABLE_SIZE;
}
void insert(int count, char* w)
{
    int pos = hash(count);
    word* newWord;
    total_words++;
    while (hashtable[pos])
    {
        if (!strcmp(hashtable[pos]->wrd, w))
        {
            hashtable[pos]->num++;
            return;
        }
        else
        {
            pos++;
        }
    }
    newWord = (word*)malloc(sizeof(word));
    newWord->num = 1;
    strcpy(newWord->wrd, w);
    hashtable[pos] = newWord;
    diff_words++;
}


int main()
{
    FILE* fp;
    char wrd_temp[80], ch;
    word* head, * curr;
    int i, arr[100], count = 0, j;

    word* temp = (word*)malloc(sizeof(word));
    memset(hashtable, 0, HASH_TABLE_SIZE);
    fp = fopen("TheGun.txt", "r");   //只读方式打开,这里输入你的文件路径
    i = 0;
    while (1)
    {
        ch = fgetc(fp);
        if (isalpha(ch) || ch == '\'')
        {
            if (islower(ch))
            {
                wrd_temp[i++] = ch - 32;  //小写字母转大写
                count = count + ch - 32;  //count用于计算单词的哈希值
            }
            else
            {
                wrd_temp[i++] = ch;
                count = count + ch;
            }
        }
        else
        {
            wrd_temp[i++] = '\0';
            if (strlen(wrd_temp) >= 1)
            {
                insert(count, wrd_temp);
            }
            memset(wrd_temp, 0, 80);
            i = 0;
            count = 0;
        }
        if (feof(fp))
            break;
    }
    fclose(fp);
    printf("Number of total_words = %d\n", total_words);
    printf("Number of different words = %d\n", diff_words);
    printf("The 100 most common words:\n");
    printf("WORD            NUMBER OF OCCURRENCES\n");
    for (i = 0; i < 10; i++)
    {
        count = 0;
        for (j = 0; j < HASH_TABLE_SIZE; j++)
        {
            if (hashtable[j] != NULL && hashtable[j]->num > count)
            {
                count = hashtable[j]->num;
                arr[i] = j;
            }
        }
        printf("%s %d\n", hashtable[arr[i]]->wrd, hashtable[arr[i]]->num);
        hashtable[arr[i]]->num = 0;
    }
    return 0;
}

#include <iostream>
#include <fstream>
#include <string>
#include <unordered_map>
#include <algorithm>
#include <queue>
#include <vector>
#include <chrono>

using namespace std;

typedef pair<string, int> value_type;

struct compare
{
public:
    bool operator()(const value_type &lhs, const value_type &rhs) const
    {
        return lhs.second > rhs.second;
    }
};

int main()
{
    string filename;
    cout << "Input File: ";
    cin >> filename;

    auto start = chrono::system_clock::now();

    ifstream file(filename);
    if (!file)
    {
        cerr << "failed to open " << filename << '\n';
        return 1;
    }

    unordered_map<string, int> freq;
    string word;
    int count = 0;
    while (file >> word)
    {
        transform(word.begin(), word.end(), word.begin(), [](auto c)
                  { return tolower(c); });
        freq[word]++;
        count++;
    }

    priority_queue<value_type, vector<value_type>, compare> queue;
    for (auto x : freq)
    {
        queue.push(x);
        if (queue.size() > 100)
            queue.pop();
    }
    vector<value_type> v;
    while (!queue.empty())
    {
        v.push_back(queue.top());
        queue.pop();
    }

    auto end = chrono::system_clock::now();
    chrono::duration<double> diff = end - start;
    cout << "Time: " << diff.count() << "s\n";
    cout << "Total Words: " << count << '\n';
    cout << "Total Different Words: " << freq.size() << '\n';
    cout << "Top 100 Words:\n";
    for (auto it = v.rbegin(); it != v.rend(); ++it)
        cout << it->first << ' ' << it->second << '\n';

    return 0;
}

比较简单的可以看看我这篇,比较容易理解修改,望采纳
#include<stdio.h>
#include<string.h>
int main()
{
char a[100],b[30];
gets(a);
gets(b);

int len1=strlen(a);
int len2=strlen(b);

int i,j,k,count=0;
for(i=0;i+len2<=len1;i++)
{
    
    for(j=i,k=0;k<len2;j++,k++)
    {
        if(a[j]!=b[k])
        break;
    }
    if(j==i+len2 && a[j] == ' ')
    count++;
}
printf("%d",count);

}