Thread: Counting Each Letter And Word In A File

  1. #1
    Registered User
    Join Date
    Jun 2020
    Posts
    1

    Post Counting Each Letter And Word In A File

    Hi guys, i need to count a file's each letter and word, find how many times a letter/word appears in file. For example "sit lorem ipsum sit amed" word has 3 m s and 2 sit s.
    Code:
    'void say(FILE *file){
    int i=0,n=0,j=0,karakterSayisi;
    fseek(file,0,SEEK_END);
    long fsize = ftell(file);
    fseek(file,0,SEEK_SET);
    char *karakterler;
    char *karakter;
    karakterler = (char *) malloc(fsize+1);
    for(i;i<fsize;i++){
    fscanf(file,"%c",&karakterler[i]);
    }
    while(!feof(file)){
    karakter = fgetc(file);
    karakterSayisi=0;
    
    
    for(n;n<fsize-1;n++)
    {
    if(karakterler[n]==karakter)
    {
    karakterSayisi++;
    }
    }
    printf("%c %d",*karakter,karakterSayisi);
    }
    }'
    i tried this but output was "L0"... so can you help me about this

  2. #2
    and the hat of int overfl Salem's Avatar
    Join Date
    Aug 2001
    Location
    The edge of the known universe
    Posts
    38,169
    Indentation matters!
    Code:
    void say(FILE * file)
    {
      int i = 0, n = 0, j = 0, karakterSayisi;
      fseek(file, 0, SEEK_END);
      long fsize = ftell(file);
      fseek(file, 0, SEEK_SET);
      char *karakterler;
      char *karakter;
      karakterler = (char *) malloc(fsize + 1);
      for (i; i < fsize; i++) {
        fscanf(file, "%c", &karakterler[i]);
      }
      while (!feof(file)) {
        karakter = fgetc(file);
        karakterSayisi = 0;
        for (n; n < fsize - 1; n++) {
          if (karakterler[n] == karakter) {
            karakterSayisi++;
          }
        }
        printf("%c %d", *karakter, karakterSayisi);
      }
    }
    > while (!feof(file))
    I'm not sure what you're expecting this to do, after the previous loop has already read the whole file into memory.
    If you dance barefoot on the broken glass of undefined behaviour, you've got to expect the occasional cut.
    If at first you don't succeed, try writing your phone number on the exam paper.

  3. #3
    Registered User
    Join Date
    May 2012
    Posts
    424
    You want to create a histogram of all characters in a file. Characters are 8 bit, so the easiest way to do this is to declare a 256-member array of ints and initialise them all to zero. Then go through the file, as you are doing, and for each use the character as an index into the array, and increment it. There's no need to store the file in memory.

    To present the results, go thrugh the array you have crated. Ignore any entries that don't correspond to a printable character. Then print the index (as a character) and the value (as an integer).
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


  4. #4
    Registered User
    Join Date
    Feb 2019
    Posts
    721
    Quote Originally Posted by Malcolm McLean View Post
    You want to create a histogram of all characters in a file. Characters are 8 bit, so the easiest way to do this is to declare a 256-member array of ints and initialise them all to zero. Then go through the file, as you are doing, and for each use the character as an index into the array, and increment it. There's no need to store the file in memory.
    Aren't you forgetting about multibyte charsets?

  5. #5
    Registered User
    Join Date
    May 2012
    Posts
    424
    Quote Originally Posted by flp1969 View Post
    Aren't you forgetting about multibyte charsets?
    He's reading the characters as bytes, so not supporting them.
    If you've a big alphabet, there's no simple efficient way of counting each character in standard C. If you use C++, there are several predefined container types you can use which will do the job.
    I'm the author of MiniBasic: How to write a script interpreter and Basic Algorithms
    Visit my website for lots of associated C programming resources.
    https://github.com/MalcolmMcLean


Popular pages Recent additions subscribe to a feed

Similar Threads

  1. Replies: 12
    Last Post: 11-24-2012, 04:10 AM
  2. Counting specific word from file
    By donaldgx in forum C Programming
    Replies: 5
    Last Post: 04-13-2012, 09:00 PM
  3. Capitalize first letter of every word in .txt file
    By crazygopedder in forum C Programming
    Replies: 9
    Last Post: 10-15-2008, 12:09 PM
  4. strtok - word counting from a file
    By |PiD| in forum C Programming
    Replies: 4
    Last Post: 11-15-2002, 04:16 AM
  5. Problem with letter and word counting
    By wordup in forum C Programming
    Replies: 3
    Last Post: 10-09-2002, 04:02 PM

Tags for this Thread