Delete repeated words in a string

Asked

Viewed 1,554 times

1

Given a string, I need to check which are the non-repeated words (or ones that appear only once) and pass those same words to a new string in order to be able to store that information.

I tried to implement a function that did that but didn’t work, and I still can’t figure out what I’m doing wrong.

int i,size=0,j;
char str[]="Eu sou o Pedro. Sou alegre e sou divertido. Tenho um carro e tenho uma casa";
///String esperada = "Eu sou o Pedro alegre e divertido tenho um carro uma casa"
size = strlen(str);
char str_aux[100];
printf("Iniciar\n");
for (i=0; i<size; i++)
{
    for (j=i+1; i<size; i++ )
    {
        while (str[i]!= ' ')
        {
            if (str[i] != str[j])
            {
                str_aux[i] = str[i];
            }
        }
    }
}
printf("%c",str_aux);
  • I don’t understand why you’re comparing the next char with the char previous, you should not compare words?

  • but comparing char to char won’t define if the word is equal, you should make a character array, I’m testing my logic here and if I can I reply already

  • 1

    In the C language, it is possible to compare strings (Character vector)... To perform it, the strcmp function is used... It will be necessary to include (#include <string.h>) Example: if(strcmp(checkpass, pass) == 0) or compare if the checkpass character vector is equal to the pass character vector.

  • You tried to use a hash table?

  • John, has the answer solved your problem? Do you need anything modified or further explained in it? If the answer is sufficient, you can mark it as correct using the on the left side of the post, this helps to show other users that the answer is right and that they can use this solution if they have the same problem as you.

  • @John Have any problems with the answer? I saw that you marked and canceled the accepted.

  • Okay, @João, I found this other function here: http://www.facom.ufu.br/~madriana/PP/TP6.pdf

Show 2 more comments

1 answer

4

Your code is very simple for its function, you are wrong because you are comparing characters and not strings, a char does not mean a string, but even if it meant the code would still be missing something, you are only comparing with the previous character, you would need to compare with all other words.

The logic I followed for this algorithm was, I declared a struct with a vector of char in, and soon after, I declared a vetor of that struct (basically a vetor of strings the size of the number of words in the sentence), in each "string" I should put a word of the sentence, but before I check if there is any equal, if there is no I add, finally I just print the whole vector that will have the whole sentence with no word repeated. The code below (You need to have a certain knowledge about functions, vectors and pointers):

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

typedef struct{ //essa struct servirá para cada palavra
    char letras[15];
}String;

int qtd_palavra(char *frase){
    int i, qtd_char, qtd_palavra = 1; //inicializo com 1 pois antes do primeiro espaço já há uma palavra

    qtd_char=strlen(frase);
    for(i=0; i<qtd_char; i++){
        if((frase[i] != '\0') && (frase[i]==' '))
            qtd_palavra++;
    }

    return qtd_palavra;
}
void main(){
    int i=0, j=0, palavras_ok=0, qtd_char; 
    int str_igual=0; //booleano para definir se a palavra é diferente de todos, 1 para verdadeiro e 0 para falso
    String palavra; //usada como "cache" para verificar se há alguma palavra igual
    char str[]="Eu sou o Pedro. Sou alegre e sou divertido. Tenho um carro e tenho uma casa";
    String palavras[qtd_palavra(str)];//defino um vetor de "strings" do tamanho da quantidade de palavras na frase

    qtd_char=strlen(str);

    for(i=0; i<=qtd_char; i++){
        if(str[i]==' '||str[i]=='\0'){ //se for o fim da palavra, no caso, se for espaço, ou fim da frase
            str_igual=0; //deixo a variavel igual a falso sempre no começo do loop
            palavra.letras[j++]=' '; //da um espaço no final da palavra
            palavra.letras[j]='\0'; //finaliza a palavra

            j=0;
            while(j<palavras_ok){
                if (strcasecmp(palavra.letras,palavras[j].letras)==0){ //se a palavra pegada for igual alguma já preenchida
                    str_igual=1; //defino como verdadeiro, caso tenha alguma palavra igual
                    break; //finaliza o ciclo pois já há uma palavra igual
                }
                j++;
            }
            if(str_igual==0){ //se não tiver nenhuma palavra igual
                strcpy(palavras[palavras_ok++].letras,palavra.letras); //copia a palavra para uma nova palavra e adiciona mais um ao numero de palavras
            }
            strcpy(palavra.letras,""); //defino a palavra que sempre uso como vazia novamente
            j=0;
        }else{
            palavra.letras[j++]=str[i];
        }
    }

    j=0;
    while(j<palavras_ok){
        printf("%s",palavras[j++].letras);
    }

    fflush(stdin);
    getchar();
}

But for the code "I" is different from "I", since strcmp is "case sensitive", I think there should be another function to compare with maiuscula and minuscula, if I find I add here.

I modified of strcmp() for strcasecmp() which is the case-insensitve function of strcmp().

Browser other questions tagged

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