Cross-Reference with vector of pointers

3

I am creating a cross reference of a vector of pointers that calls a simple linked list. Aim is to type a sentence and decompose your 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]!='
    #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]!='%pre%'; 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]='%pre%';    
}
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);
}
'; 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]='%pre%'; } 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 writing the same word in the 2x list. I tried to put the printf so I could see if I found the problem, it is entering when it is null list, and is running printf 54 that should not, because once tested above, if the word is equal it should only increment the counter and return . If anyone can help me, thank you!

    
asked by anonymous 12.10.2018 / 16:22

1 answer

2

When you debug a program, you generally find the error with some ease. In this case, the sentence that is read with fgets leaves the \n in the string, as expected. For this reason when you read the sentence:

carro casa carro

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

carro
casa
carro\n

And so the last carro is different from the first, although visually it does not look like it. You can easily test this by either printing the% wt% you received, or testing with a word that does not repeat itself at the end.

To correct it, just add the following to the string reading of the instruction to remove the remaining line break:

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

The fgets finds the position of what was passed in the second parameter, in this case the line break and causes the string to terminate at that location by putting strcspn in that position.

    
12.10.2018 / 17:49