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

  • Thread starter Thread starter Yamna
  • Start date Start date
  • Tags Tags
    Lang
Click For Summary

Discussion Overview

The discussion revolves around creating a C program to count the number of characters and words in a file. Participants explore different approaches to file reading and character processing, addressing potential pitfalls in counting words accurately.

Discussion Character

  • Homework-related
  • Technical explanation
  • Debate/contested

Main Points Raised

  • One participant requests assistance in developing a program that counts characters and words in a file.
  • A proposed algorithm suggests reading the file character by character, incrementing a character counter for each character read, and a word counter when encountering a blank space.
  • Another participant points out that counting blank spaces does not accurately reflect word counts, as multiple spaces can separate words and files may contain only whitespace.
  • A participant shares their implementation of the algorithm but reports that it is not functioning correctly, providing their code for review.

Areas of Agreement / Disagreement

Participants express differing views on the accuracy of the proposed word counting method, indicating that the discussion contains unresolved disagreements regarding the best approach to counting words in a file.

Contextual Notes

The discussion highlights limitations in the proposed algorithm, such as the handling of multiple whitespace characters and the potential for files containing only whitespace. There are also unresolved issues in the shared code, including the use of incorrect functions and conditions.

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();
}
 

Similar threads

  • · Replies 2 ·
Replies
2
Views
2K
Replies
1
Views
6K
  • · Replies 1 ·
Replies
1
Views
3K
Replies
35
Views
7K
  • · Replies 12 ·
Replies
12
Views
3K
  • · Replies 33 ·
2
Replies
33
Views
3K
Replies
7
Views
3K
Replies
81
Views
7K
  • · Replies 3 ·
Replies
3
Views
3K
  • · Replies 12 ·
Replies
12
Views
2K