【问题标题】:How to count word occurrences each different word in C如何计算C中每个不同单词的单词出现次数
【发布时间】:2020-05-14 06:00:59
【问题描述】:

程序应以相同的顺序包含表中的单词 它们出现在文本中。 使用string.h、ctype.h、stdio.h,包含strtok函数

#include<ctype.h>
int main(void)
{
    int i,j;
    char text[3][80];
    char wordList[120][80];
    int count = 0;
    char* ptr;

    for (i = 0; i <= 2; i++) {
        gets(&text[i][0]);
    }
    for (i = 0; i <= 2; i++) {
        for (j = 0; text[i][j]!='\0' ; j++) {
            text[i][j] = tolower(text[i][j]);
        }
    }
    ptr = strtok(text, " ,.;:!?-()[]<>");
    while (ptr != NULL) {

    }

想了很久,不知道怎么试。你可以问我的代码有什么问题,但我根本不知道方法......

【问题讨论】:

  • 在更进一步之前,请先查看Why gets() is so dangerous it should never be used!,并认真思考是否gets(您将替换为fgets())——不能简单地使用text[i] 而不是&amp;text[i][0]
  • 方法:1)使用strtok获取下一个单词。 2) 检查单词是否已经在wordList 中。 3) 如果找到,则增加该单词的计数。 4) 如果没有找到,将单词添加到wordList 并将计数器设置为 1。只要输入中有单词就重复此操作。 5) 打印wordList 和相应的计数。

标签: c arrays string pointers word


【解决方案1】:

试试这个...

#include <stdio.h>
#include <string.h>
void main()
{
int count = 0, c = 0, i, j = 0, k, space = 0;
char str[100], p[50][100], str1[20], ptr1[50][100];
char *ptr;

printf("Enter the string\n");
scanf(" %[^\n]s", str);

for (i = 0;i<strlen(str);i++)
    if ((str[i] == ' ')||(str[i] == ',' && str[i+1] == ' ')||(str[i] == '.'))
        space++;

for (i = 0, j = 0, k = 0;j < strlen(str);j++)
{
    if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))  
    {    
        p[i][k] = '\0';
        i++;
        k = 0;
    }        
    else
         p[i][k++] = str[j];
}

k = 0;

for (i = 0;i <= space;i++)
{
    for (j = 0;j <= space;j++)
    {
        if (i == j)
        {
            strcpy(ptr1[k], p[i]);
            k++;
            count++;

            break;
        }
        else
        {
            if (strcmp(ptr1[j], p[i]) != 0)
                continue;
            else
                break;
        }
    }
}

for (i = 0;i < count;i++) 
{
    for (j = 0;j <= space;j++)
    {
        if (strcmp(ptr1[i], p[j]) == 0)
            c++;
    }
    printf("%s -> %d times\n", ptr1[i], c);
    c = 0;
}
}

【讨论】:

【解决方案2】:

试试这个

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

void main()
{
    int count = 0, c = 0, i, j = 0, k, space = 0;

    char str[100], p[50][100], str1[20], ptr1[50][100];

    char *ptr;

    printf("Enter the string\n");
    scanf(" %[^\n]s", str);
    for (i = 0;i<strlen(str);i++)
    {
        if ((str[i] == ' ')||(str[i] == ',' && str[i+1] == ' ')||(str[i] == '.'))
        {
            space++;
        }
    }

    for (i = 0, j = 0, k = 0;j < strlen(str);j++)
    {
        if ((str[j] == ' ')||(str[j] == 44)||(str[j] == 46))  
        {    
            p[i][k] = '\0';
            i++;
            k = 0;
        }        
        else
             p[i][k++] = str[j];
    }

    k = 0;

    for (i = 0;i <= space;i++)
    {
        for (j = 0;j <= space;j++)
        {
            if (i == j)
            {
                strcpy(ptr1[k], p[i]);
                k++;
                count++;

                break;
            }
            else
            {
                if (strcmp(ptr1[j], p[i]) != 0)
                    continue;
                else
                    break;
            }
        }
    }

    for (i = 0;i < count;i++) 
    {
        for (j = 0;j <= space;j++)
        {
            if (strcmp(ptr1[i], p[j]) == 0)
                c++;
        }
        printf("%s -> %d times\n", ptr1[i], c);
        c = 0;
    }
}

【讨论】:

    猜你喜欢
    • 2021-04-19
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-06-28
    相关资源
    最近更新 更多