-2

image[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...

Support Ukraine
  • 42,271
  • 4
  • 38
  • 63
sjfefqbn
  • 13
  • 4
  • 2
    Before going further, see [Why gets() is so dangerous it should never be used!](https://stackoverflow.com/questions/1694036/why-is-the-gets-function-dangerous-why-should-it-not-be-used) and think real hard about whether `gets` (you will replace with `fgets()`) -- can't simply use `text[i]` instead of `&text[i][0]`? – David C. Rankin May 14 '20 at 06:16
  • 1
    Approach: 1) Use `strtok` to get the next word. 2) Check if the word is already in the `wordList`. 3) If found, increment the count for that word. 4) If not found, add the word to the `wordList` and set the counter to 1. Repeat this as long as there are words in the input. 5) Print the `wordList` and the corresponding count. – Support Ukraine May 14 '20 at 06:24
  • [Count the reocurrence of words in text file](https://stackoverflow.com/a/43445812/3422102) may also be helpful... – David C. Rankin May 14 '20 at 06:24

2 Answers2

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;
}
}
  • 2
    Unless you are programming in a *freestanding environment* (without the benefit of any OS), in a standards conforming implementation, the allowable declarations for `main` for are `int main (void)` and `int main (int argc, char *argv[])` (which you will see written with the equivalent `char **argv`). See: [C11 Standard - §5.1.2.2.1 Program startup(p1)](http://port70.net/~nsz/c/c11/n1570.html#5.1.2.2.1p1). See also: [What should main() return in C and C++?](http://stackoverflow.com/questions/204476/) – David C. Rankin May 14 '20 at 07:24
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;
    }
}