Conflict between counting words from a file and other functions

Asked

Viewed 15 times

0

I need to make a progress as described in the image. I was able to do all the steps, but I couldn’t get the word count to run along with the other processes.

inserir a descrição da imagem aqui

I followed the code:

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

FILE *arq;

int ContarPalavras(char *sub)
{
    char frase[100];
    int qntd = 0;
    
    while(!feof(arq))
    {
        fgets(frase, 100, arq);
        sub = strtok(frase, " .!,");
        while(sub != NULL)
        {
            sub = strtok(NULL, " ");
            qntd++;
        }
    }
    
    return qntd;
}

int main(void)
{
    int qntdCarac = 0, qntdLinhas = 0, qntdPalavras = 0;
    char c;
    char *sub;
    int a = 0, b = 0, C = 0, d = 0, e = 0, f = 0, g = 0, h = 0;
    int i = 0, j = 0, k = 0, l = 0, m = 0, n = 0 ,o = 0, p = 0;
    int q = 0, r = 0, s = 0, t = 0, u = 0, v = 0, w = 0, x = 0, y = 0, z = 0;
    
    arq = fopen("doc.txt", "r");
    
    while((c = getc(arq)) != EOF)
    {
        qntdCarac++;
        if(c == '\n')
        {
            qntdLinhas++;
        } else if(c == ' ')
            {
                qntdPalavras++;
            }
        switch(c)
            {
                case 'a':
                    a++;
                    break;
                case 'b':
                    b++;
                    break;
                case 'c':
                    C++;
                    break;
                
                case 'd':
                    d++;
                    break;
                case 'e':
                    e++;
                    break;
                case 'f':
                    f++;
                    break;
                case 'g':
                    g++;
                    break;
                case 'h':
                    h++;
                    break;
                case 'i':
                    i++;
                    break;
                case 'j':
                    j++;
                    break;
                case 'k':
                    k++;
                    break;
                case 'l':
                    l++;
                    break;
                case 'm':
                    m++;
                    break;
                case 'n':
                    n++;
                    break;
                case 'o':
                    o++;
                    break;
                case 'p':
                    p++;
                    break;
                case 'q':
                    q++;
                    break;
                case 'r':
                    r++;
                    break;
                case 's':
                    s++;
                    break;
                case 't':
                    t++;
                    break;
                case 'u':
                    u++;
                    break;
                case 'v':
                    v++;
                    break;
                case 'w':
                    w++;
                    break;
                case 'x':
                    x++;
                    break;
                case 'y':
                    y++;
                    break;
                case 'z':
                    z++;
            }
        
        qntdPalavras = ContarPalavras(sub);
        
    }
    
    printf("Qntd de caracteres : %d\nqntd de linhas: %d\n"
           "qntd de palavras = %d\n\n", qntdCarac-qntdLinhas, qntdLinhas+1, qntdPalavras);
    printf( "a = %d, b = %d, c = %d, d = %d,\n e = %d, f = %d, g = %d, h = %d,\n "
            "i = %d, j = %d, k = %d, l = %d,\n m = %d, n = %d ,o = %d, p = %d,\n "
            "q = %d, r = %d, s = %d, t = %d,\n u = %d, v = %d, w = %d, x = %d,\n "
            "y = %d, z = %d", 
            a, b, C, d, e, f, g, h, i, j, k, l, m, n ,o, p, q, r, s, t, u, v, w, x, y, z );
    
    
    fclose(arq);

    return 0;
}
No answers

Browser other questions tagged

You are not signed in. Login or sign up in order to post.