Cross-Reference with vector of pointers

Question:

I'm creating a cross reference of a vector of pointers that calls a simple linked list. Objective is to type a sentence and decompose its words, placing them in each position of the vector according to their respective initial letter.

    #include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include <time.h>
#include <locale.h>
#include<errno.h>
#include<time.h>
#include<conio.h>


void Menu()
{
                     printf("\n\nBem vindo ao programa de texto\n");
                     printf("------------------------------------------------------------------------------");
                    printf("\n1 - DIGITAR TEXTO                                                            |"); 
                    printf("\n2 - IMPRIMIR TODAS AS PALAVRAS                                               |");
                    printf("\n3 - LISTAR PALAVRAS DE UMA LETRA                                             |");
                    printf("\n4 - QUANTIDADE TOTAL DE PALAVRAS                                             |");
                    printf("\n5 - REMOVER  PALAVRA                                                         |");
                    printf("\n6 - REFERENCIA TOTAL                                                         |");
                    printf("\n7 - REFERENCIA DE UMA LETRA                                                  |");
                    printf("\n8 - PALAVRA COM MAIOR QUANTIDADE                                             |");
                    printf("\n0 - SAIR DO PROGRAMA                                                         |");
                    printf("\n------------------------------------------------------------------------------");
                     printf("\n");
}


struct Palavra
{
    char palavra[40];
    int quant;
    struct Palavra *next;

};

struct Palavra * referencia[26];

void InsereLetra(char palavra[])
{
    int pos;
    pos=palavra[0]-'A';
    printf("\n POS:%d",pos);
    if(referencia[pos]==NULL)
    {
        printf("\nestou aqui");

        struct Palavra * p = (struct Palavra *)malloc(sizeof(struct Palavra ));
        strcpy(p->palavra,palavra);
        p->next=NULL;
        p->quant=1;
        referencia[pos]=p;


    }else
    {
        struct Palavra * aux=referencia[pos];
        struct Palavra * ant=aux;
        struct Palavra * ant2=aux;
        while(ant!=NULL)
        {
            if(strcmp(palavra,ant->palavra)==0)
            {
                //printf("\nestou aqui2 cont %d",ant->quant);
                (ant->quant)++;
                printf("\nestou aqui cont %d\n",ant->quant);
                return ;

            }
            ant=ant->next;
        }
        while(ant2->next!=NULL)
        {
            printf("\nestou aqui3");
            ant2=ant2->next;
        }
        printf("\nestou aqui54\n");
            struct Palavra * p = (struct Palavra *)malloc(sizeof(struct Palavra ));
        strcpy(p->palavra,palavra);
        p->next=NULL;
        p->quant=1;
        ant2->next=p;
    return ;    
    }

}


void convert(char aux[])
{

    int i;
for( i=0;aux[i]!='\0'; i++)
    {
        aux[i]= toupper(aux[i]);

        if(aux[i]=='Ã'||aux[i]=='Á'||aux[i]=='Ä'||aux[i]=='À'||aux[i]=='Â')
    {
        aux[i]='A';
    }
    if(aux[i]=='É'||aux[i]=='È'||aux[i]=='Ë'||aux[i]=='Ê')
    {
        aux[i]='E';
    }
    if(aux[i]=='Í'||aux[i]=='Ì'||aux[i]=='Î'||aux[i]=='Ï')
    {
        aux[i]='I';
    }
    if(aux[i]=='Õ'||aux[i]=='Ó'||aux[i]=='Ö'||aux[i]=='Ò'||aux[i]=='Ô')
    {
        aux[i]='O';
    }
    if(aux[i]=='Ú'||aux[i]=='Ù'||aux[i]=='Ü'||aux[i]=='Û')
    {
        aux[i]='U';
    }

    }
    aux[i]='\0';    
}
int main()
{
    char ck,c;  
    int i,cont=0;
    for(i=0;i<=25;i++)
    {
        referencia[i]=NULL;
    }

    do{
        Menu();
    char frase[500],frase2[500];
        scanf("%d",&i);
        switch(i)
        {
                            case 1:
                                        setbuf(stdin, NULL);
                                        printf("\nDigite a frase:");



                                        fgets(frase,500,stdin);
                                            convert(frase);
                                        printf("\n");
                                            char * pch;
                                         printf ("Splitting string \"%s\" into tokens:\n",frase);
                                         pch = strtok (frase," ,.-:?;");
                                         while (pch != NULL)
                                         {
                                            //palavra
                                          printf ("%s\n",pch);
                                          //head=
                                          InsereLetra(pch);
                                          //LETRA
                                         // printf("%c\n",pch[0]);
                                          pch = strtok (NULL," ,.-:?;");
                                          }
                                setbuf(stdin, NULL);
                             break;

                                case 2:
                                        printf("\nIMPRIMIR TODAS AS PALAVRAS");
                                    //   imprimirPalavras(head);
                                    for(i=0;i<=25;i++)
                                       {

                                        if(referencia[i]!=NULL)
                                           {
                                            struct Palavra * aux=referencia[i];
                                            while(aux!=NULL)
                                               {
                                                printf("\n%s",aux->palavra);

                                                aux=aux->next;
                                               }
                                           }
                                       }
                                 break;

                                case 3: 
                                     printf("\nLISTAR PALAVRAS DE UMA LETRA");
                                     printf("\nDigite uma letra");
                                     scanf("%c",&ck);
                                //   listLetra(head,ck);

                                break;  
                                        case 4:
                                        printf("\n QUANTIDADE TOTAL DE PALAVRAS");
                                        //quantPalavras(head);
                                        break;
                                 case 5:
                                    printf("\nREMOVER PALAVRA");

                                 break;
                                     case 6:
                                    printf("\nREFERENCIA TOTAL");
                                //  refPalavras(head);
                                //Imprimir
                                     break;
                                 case 7:
                                    printf("\nREFERENCIA POR LETRA");
                                //Imprimir
                                    printf("\nDigite uma letra:");
                                    scanf("%c",&c);
                                //  refLetra(head,c);
                                 break;
                                     case 8:
                                    printf("\nPALAVRA COM MAIOR QUANTIDADE");   
                                //  maiorQuant(head);

                                     break;

                                case 0:
                                    printf("\n\nSaindo do programa...\n");
                                 break;


        }
    }while(i!=0);
}

The problem I'm having is typing 2 or more times the same word in a sentence, regardless of how many times the program is recording 2x the same word in the list. I tried to put the printf so I could see if I found the problem, it's entering when it's a nulla list, and it's running printf 54 which it shouldn't, because once tested above, if the word is the same it should just increment the counter and return . If anyone can help me, I appreciate it!

Answer:

When you debug a program, you usually find the error fairly easily. In this case it turns out that the sentence that is read with fgets leaves you the \n in the string, as you would expect. That's why when you read the sentence:

carro casa carro

In fact the split will give you the following 3 words:

carro
casa
carro\n

And that's why the last carro is different from the first, although visually it doesn't look like it. You can test this easily either by printing the string you received, or by testing it with a word that doesn't repeat itself at the end.

To correct it, just add the instruction to remove the line break that remained below, reading fgets :

fgets(frase,500,stdin);
int posicao_quebra = strcspn(frase, "\n"); // achar onde está a quebra de linha
frase[posicao_quebra] = '\0'; // fazer a string terminar na quebra

strcspn finds the position of what was passed in the second parameter, in this case the line break, and makes the string end there by putting the \0 that position.

Scroll to Top