Estou criando uma cross reference de um vetor de ponteiros que chama uma lista encadeada simples. Objetivo é digitar uma frase e decompor suas palavras, colocando elas em cada posição do vetor de acordo com sua respectiva letra inicial.
#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);
}
O problema que estou tendo é digitar 2 ou mais vezes a mesma palavra em uma frase, independente de quantas vezes for o programa está gravando na lista 2x a mesma palavra. Tentei colocar os printf para eu poder ver se achava o problema, ele está entrando quando é lista nulla, e está executando o printf 54 que não deveria, pois uma vez testado acima, se a palavra for igual ele só deveria incrementar o contador e retornar. Se alguém puder me ajudar, agradeço!