How to Count Characters and Words in a File Using C?

  • Thread starter Thread starter Yamna
  • Start date Start date
  • Tags Tags
    Lang
AI Thread Summary
The discussion centers on creating a program to count characters and words in a file, with an initial pseudo code outline provided. The key steps include reading the file character by character, incrementing a character counter for each character read, and increasing a word counter when a whitespace character is encountered. However, it is noted that simply counting whitespace does not accurately reflect word count, as multiple whitespace characters can separate words, and files may contain only whitespace without any words.The user shares a C program attempting to implement this logic but encounters errors. The program opens a file and uses a loop to read characters, counting letters, words, numbers, and special characters. Issues identified in the code include improper file opening checks and incorrect usage of the `getch` function instead of `fgetc` for reading characters. The program's logic for counting words needs refinement to ensure accurate results. Overall, the discussion highlights the complexities of accurately counting words and characters in a file and the need for precise coding practices.
Yamna
Messages
4
Reaction score
0
hi.
please can anyone help me in making a program that opens a file and tell the no. of character and no. of words in the file.
i can write a program to open a file but how can we count characters and words of any file..:confused:
 
Technology news on Phys.org
In Pseudo code:

1 - Create int for number of ****characters****
2 - Create int for number of ****words****

1 - Read the File
2 - Process the File character by character in a loop until EOF is reached
3 - Increase counter for ****characters**** with every character read
4 - Compare character you read into a blankspace. If character = blankspace then increase counter for ****words****

Once the while loop hits EOF (end of file) you will have a count of characters (every time the loop runs) and words (every time the read characters equals a blank space).
 
DwithQs said:
4 - Compare character you read into a blankspace. If character = blankspace then increase counter for ****words****

That counts the number of "blankspace" characters, (I suppose you meant "whitespace") not the number of words.

Two words can be separated by more than one whitespace character.

Also the file might contain only whitespace characters, and no "words" at all.
 
True, it is nearly limitless on things to check for to get an accurate word count.
 
i have made a program with this algorithm but its not working properly my code is given below can you help me to remove the errors
Code:
#include<stdio.h>
#include<conio.h>

void main()
{
	FILE *f;
	char ch;
	int nalpha=0,nword=0,nno=0,nsc=0;
	f=fopen("abc.txt","r");
	if(f!='NULL')
	{
		while(ch=getch(f)!=EOF)
		{
			printf("%c",ch);
			if((ch>='a'&&ch<='z')||(ch>='A'&&ch<='Z'))
				nalpha++;
			else if((ch==' ')||(ch=='.'))
				nword++;
			else if(ch>='0'&&ch<='9')
				nno=nno+1;
			else
				nsc++;

		}
	}
	printf("no.of alphabets is %d",nalpha);
	printf("no.of words is %d",nword);
	printf("no.of numbers is %d",nno);
	printf("no.of other characters is %d",nsc);
	fclose(f);
	getch();
}
 
Dear Peeps I have posted a few questions about programing on this sectio of the PF forum. I want to ask you veterans how you folks learn program in assembly and about computer architecture for the x86 family. In addition to finish learning C, I am also reading the book From bits to Gates to C and Beyond. In the book, it uses the mini LC3 assembly language. I also have books on assembly programming and computer architecture. The few famous ones i have are Computer Organization and...
I have a quick questions. I am going through a book on C programming on my own. Afterwards, I plan to go through something call data structures and algorithms on my own also in C. I also need to learn C++, Matlab and for personal interest Haskell. For the two topic of data structures and algorithms, I understand there are standard ones across all programming languages. After learning it through C, what would be the biggest issue when trying to implement the same data...
Back
Top