Click here to Skip to main content
15,889,862 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I'm writing a code to find the frequency of each alphabet in a file. But I don't get the right answer. Can you please tell me what's wrong with it?

What I have tried:

C
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define tab_size 26
struct alphabet{
	char alpha;
	int count;
};
struct alphabet table[tab_size];
int main(){
	FILE *fptr;
	int buf_size=1000,i,j;
	char name[100],buffer[1000],letter='a';
	printf("\nEnter the file name:");
	scanf("%s",name);
	fptr=fopen(name,"r");
	if(fptr==NULL){
		printf("\nProblem with opening the file");
		exit(1);
	}
	for (i = 0; i < tab_size; i++) {
    	table[i].alpha = letter++;
    	table[i].count = 0;
    //	printf("%c\n",table[i].alpha);
	}
//	fread(buffer,1,buf_size,fptr);
//content is available in the buffer from 0 to 999
while(feof(fptr)==0){
	fread(buffer,1,buf_size,fptr);
	for(i=0;i<buf_size;i++){
		j=0;
		if(isalpha(buffer[i])){
			while((buffer[i]!=table[j].alpha||buffer[i]!=toupper(table[i])&&j<tab_size){
			j++;
		}
		table[j].count++;
	}
	else
		continue;
	}
}
for(i=0;i<tab_size;i++){
	printf("Count of %c is %d\n",table[i].alpha,table[i].count);
}
}
Posted
Updated 16-Jul-20 2:38am
v2
Comments
Patrice T 16-Jul-20 7:21am    
Show sample input, actual output and output expected.

You are counting the wrong numbers. This works for characters
C++
int table[26] = {0};
// get a valid characters (exclude spaces and numbers)
char c = toupper(buffer);
int index = c - 'A';
table[index]++;
Use the debugger to see the details.
 
Share this answer
 
Comments
Sharon Shelton 17-Jul-20 14:18pm    
Thank you. It was helpful
I've fixed your code (to same extent: it should work, but, as it stands, it neither particularly robust nor efficient). Try
C
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>

#define tab_size 26

enum
{
  TABLE_SIZE = 26,
  BUFFER_SIZE = 1024,
  STRING_SIZE = 256,
};

struct alphabet
{
  char alpha;
  int count;
};

struct alphabet table[tab_size];

int main()
{
  FILE *fptr;
  int i;

  char filename[STRING_SIZE];
  char buffer[BUFFER_SIZE];
  char letter = 'a';
  printf("\nEnter the file name:");
  scanf("%s", filename);
  fptr = fopen( filename, "r");
  if ( !fptr )
  {
    printf("\nProblem with opening the file");
    exit(1);
  }

  for (i = 0; i < TABLE_SIZE; i++)
  {
      table[i].alpha = letter++;
      table[i].count = 0;
  }

  size_t nread;

  while ( ( nread = fread(buffer, 1, BUFFER_SIZE, fptr) ) > 0)
  {
    for(i=0; i<nread; ++i)
    {
      if( isalpha(buffer[i]) )
      {
        int j;
        for (j = 0; j<TABLE_SIZE; ++j)
        {
          if ( buffer[i] == table[j].alpha || buffer[i] == toupper(table[j].alpha))
          {
              ++table[j].count;
              break; // exit the j-loop
          }
        }
      }
    }
  }

  for(i = 0; i<TABLE_SIZE; ++i)
  {
    printf("Count of %c is %d\n",table[i].alpha,table[i].count);
  }

  fclose(fptr);

  return 0;
}
 
Share this answer
 
Comments
Patrice T 16-Jul-20 8:33am    
+5
CPallini 16-Jul-20 8:46am    
Thank you.
Sharon Shelton 16-Jul-20 10:23am    
Thanks for your help!
CPallini 16-Jul-20 10:28am    
You are welcome.
You haven't stated what the code is producing ... maybe you should use Improve question

In the meantime, look at these two bold bits

char name[100],buffer[1000],letter='a';

...
while((buffer[i]!=table[j].alpha||buffer[i]!=toupper(table[i])&&j<tab_size){


So even if I agreed with the rest of the logic, maybe you want to think about this
 
Share this answer
 
Quote:
I'm writing a code to find the frequency of each alphabet in a file. But I don't get the right answer. Can you please tell me what's wrong with it?

Your code do not behave the way you expect, or you don't understand why !

There is an almost universal solution: Run your code on debugger step by step, inspect variables.
The debugger is here to show you what your code is doing and your task is to compare with what it should do.
There is no magic in the debugger, it don't know what your code is supposed to do, it don't find bugs, it just help you to by showing you what is going on. When the code don't do what is expected, you are close to a bug.
To see what your code is doing: Just set a breakpoint and see your code performing, the debugger allow you to execute lines 1 by 1 and to inspect variables as it execute.

Debugger - Wikipedia, the free encyclopedia[^]

Mastering Debugging in Visual Studio 2010 - A Beginner's Guide[^]
Basic Debugging with Visual Studio 2010 - YouTube[^]

1.11 — Debugging your program (stepping and breakpoints) | Learn C++[^]

The debugger is here to only show you what your code is doing and your task is to compare with what it should do.
-----
Advice: Learn to indent properly your code, it show its structure and it helps reading and understanding. It also helps spotting structures mistakes.
C++
#include <stdio.h>
#include <ctype.h>
#include <stdlib.h>
#include <string.h>
#define tab_size 26
struct alphabet{
    char alpha;
    int count;
};
struct alphabet table[tab_size];
int main(){
    FILE *fptr;
    int buf_size=1000,i,j;
    char name[100],buffer[1000],letter='a';
    printf("\nEnter the file name:");
    scanf("%s",name);
    fptr=fopen(name,"r");
    if(fptr==NULL){
        printf("\nProblem with opening the file");
        exit(1);
    }
    for (i = 0; i < tab_size; i++) {
        table[i].alpha = letter++;
        table[i].count = 0;
        //  printf("%c\n",table[i].alpha);
    }
    //  fread(buffer,1,buf_size,fptr);
    //content is available in the buffer from 0 to 999
    while(feof(fptr)==0){
        fread(buffer,1,buf_size,fptr);
        for(i=0;i<buf_size;i++){
            j=0;
            if(isalpha(buffer[i])){
                while((buffer[i]!=table[j].alpha||buffer[i]!=toupper(table[i])&&j<tab_size){
                    j++;
                }
                table[j].count++;
            }
            else
                continue;
        }
    }
    for(i=0;i<tab_size;i++){
        printf("Count of %c is %d\n",table[i].alpha,table[i].count);
    }
}

Indentation style - Wikipedia[^]

Professional programmer's editors have this feature and others ones such as parenthesis matching and syntax highlighting.
Notepad++ Home[^]
ultraedit[^]
 
Share this answer
 
Comments
Sharon Shelton 16-Jul-20 10:24am    
Thank you! Will do my best to improve

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900