Frequency counter using c ++ files

1

Good morning

I'm doing a frequency counter, where indicating the address of the file (txt) show which are the letters that appear and the number of times they appear in the text. The problem I have is in the counter which does not show the letters found or the number of repeated characters.

#include <iostream.h>
#include <stdio.h>

void contar(char frase[], char diccionario[], int);
int letras;

main(){
    FILE *archivo;
   char frase[100], diccionario[86] ="ABCDEFGHIJKLMNÑOPQRSTUVWXYZabcdefghijklmnñopqrstuvwxyz0123456789áéí[]{}/%!$&:;,.<>=?¿'" ;
   int caracter,caracteres,i;



   cout<<"Ingrese la direccion sin la extension (.txt):\n";
   gets(frase);
   strcat(frase,".txt");
   archivo=fopen(frase, "r");

   while((caracter = fgetc(archivo)) != EOF){
    printf("%c", caracter);
    letras++;
    for(i=0;i<86;i++){
        contar(frase, diccionario,i);
    }
   }

   printf("\n\n\nLa cantidad de letras es: %d",letras);
   fclose(archivo);
   getchar();
   return 0;
}

void contar(char frase[], char diccionario[],int i){
  int j,num,contador;
  char letra;
  num = letras;
  for(j=0;j<num-1;j++){
    if(frase[j]==diccionario[i]){
    contador+=1;
      letra = diccionario[i];
   }
  }
  if (contador !=0){
    printf("el numero de letras %c es %d\n", letra, contador);
  }
}

This is what the program shows me running when I use a file which has the word "anona"

    
asked by GersonRod_ 07.05.2017 в 07:58
source

1 answer

1

Is it c ++? it seems C ... actually it seems a mixture between both, as if you took parts of a code C and you implanted it in c ++

Let me guess ... The file you are reading begins with the letter l?

The problem I see is in the arguments that you are passing to the function contar

        // frase es el nombre del archivo, no su contenido
        contar(frase, diccionario,i);

To be able to send the content to the count function you have to first store it

// almaceno el archivo dentro de frase
letras = 0;
while((caracter = fgetc(archivo)) != EOF)
{
    printf("%c", caracter);
    frase[letras] = caracter
    letras++;
}
// luego y fuera del while verifico las letras de frase 
for(i=0;i<86;i++)
{
    contar(frase, diccionario,i);
}

This should solve your problem as it is posed, however I think you could use the library #include <string> to manage the character strings and the library #include <fstream> to handle files and it could be easier though < em> this is an opinion.

Greetings.

    
answered by 07.05.2017 / 13:13
source