简体   繁体   中英

How to count word occurrences each different word in C

图片[1]

Program should include the words in the table in the same order in which they appear in the text. Use string.h, ctype.h, stdio.h, include strtok function

#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) {

    }

I've been thinking for a long time, and I don't know how to try. You could ask me what's wrong with my code, but I don't know the approach at all...

try this...

#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;
}
}

try this

#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;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM