Cross-Reference with pointer array

Asked

Viewed 47 times

3

I’m creating a cross Reset of a pointer vector that calls a simple chained list. Goal is to type a sentence and decompose your words, placing them in each vector position 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 the same word in the 2x list. I tried to put the printf so I could see if I thought the problem, it is entering when it is null list, and it 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, I’d appreciate it!

1 answer

2


When you debug a program, you usually find the error with some ease. In the case it happens that the phrase that is dealt with fgets let him the \n in the string, as expected. For this reason when reading the phrase:

carro casa carro

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

carro
casa
carro\n

That’s why the last one carro is different from the first, although visually it doesn’t look like it. It can easily test this by printing the string which he received, either by testing with a word that does not repeat at the end.

To correct just add the reading of the following fgets the instruction to remove the line break that got:

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

The strcspn finds the position of what was passed in the second parameter, in this case the line break and causes the string to end in this location by placing the \0 in that position.

  • thank you very much helped me a lot!!! It’s working 100%! I didn’t know this strcspn function at first added it and it’s 100%

Browser other questions tagged

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