How to calculate the number of times a word appears within a sentence

Asked

Viewed 364 times

0

I’m trying to create a code in which I have to count the number of times a word appears within a sentence, but after a lot of searching and searching about, I can only find codes that use the fgets function, I was wondering if you would know why else, I could replace the fgets and still run the whole. That’s the code I’ve developed for now :

#include <stdio.h>

int main()
{

char frase[250];
char palavra[50];
int i;
int j = 0;
int count = 0;

    printf( "escveva uma frase: " );
    scanf( "%[^\n]", frase );
    printf( "escreve uma palavra: " );
    scanf( "%s", palavra );

for(int i = 0; frase[i] != '\0'; i++)

    if (palavra[j] == frase[i])
    {
        j += 1;
        if (palavra[j] == '\0')
        {
            count += 1;
            j == 0;
        }
    }
    else
    {
        j = 0;
    }

    printf( "count = %d\n", count);


    return 0;

}

On top of everything else, I believe I’m doing something wrong on the go or on the other side, because I’m not getting the result I wanted, but I can’t understand where I might be going wrong. If you can help me understand my mistake, and what else besides fgets could be used, I would be very grateful. Besides, I believe I have to put some strings, like strlen, but I only learned to use them with characters

  • Several wrong things. It starts with confusion between the assignment operator = and the comparison operator for equality ==. Do not use the & in the scanf function for string reading.

  • Thank you, now I did!! I didn’t understand the reason for the mistake, and now it worked

2 answers

2

Repetitions of a Word

To achieve the number of repetitions you can search the base string for another target string, and that’s exactly what the function strstr() library string.h faz (vide strstr, in English). Although the function finds only the first occurrence, you can repeat the process until you reach the end of the string by means of a while checking whether the function did not return a null pointer (signal that it no longer found the target string, requires library inclusion stdlib.h), however, it is necessary that, with each function search, you increment the pointer of the target string, it will give the impression that it is shrinking, but in fact you are only changing the indication of where it starts so that the strstr() can continue where you left off. See the following code:

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

int main(int argc, char ** argv)
{
    char * frase = "todo mes, todo dia, todo minuto, todo segundo\0";
    char * inicio_frase = frase;
    char * busca = "todo\0";
    int repeticoes = 0;
    do
    {
        frase = strstr(frase, busca);
        if(frase != NULL)
        {
            repeticoes++;
            printf("Achou: %s\n", frase);
        }
        frase++;
    } while(strstr(frase, busca) != NULL);
    frase = inicio_frase;
    printf("Resultados ===================================\n");
    printf("Frase: %s\n", frase);
    printf("Busca: %s\n", busca);
    printf("Repetições da Busca na Frase: %d\n", repeticoes);
    return EXIT_SUCCESS;
}

Upshot:

inserir a descrição da imagem aqui

Alternative to fgets()

It is disclosed that the function gets() is dangerous and should not be used because there is no treatment for excessive reading memory leakage (see gets is of risky use, in English). The safest and most practical alternative to gets() is just the fgets() because it will only read the input characters up to the set limit. However, I don’t know if you have any VERY specific restrictions with this code for not using the fgets(), then you can implement your own line reading function:

void ler_linha(char * string_recipiente, int tamanho_maximo)
{
    char entrada = '\0';
    int posicao = 0;
    do
    {
        entrada = getchar();
        if(entrada != '\n')
            string_recipiente[posicao] = entrada;
        posicao++;
    } while(entrada != '\n' && posicao < (tamanho_maximo - 1));
    string_recipiente[posicao] = '\0';
}

The filter [ n] is dangerous

Just like using gets() is dangerous, use scanf() with the filter [^\n] it is also because there is no limit treatment, bringing back all the evils of gets().

0

your mistake is in this line:

  if (palavra[j] = frase[i])

is assigning, not comparing, the operator you want is ==

  if (palavra[j] == frase[i])

And at the time of reading for strings you don’t need the &. Your compiler probably is giving Warning sobbre this.

Browser other questions tagged

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