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

  • Thread starter Yamna
  • Start date
  • Tags
    Lang
In summary, the program would count the number of characters, words, numbers, and other characters in a file.
  • #1
Yamna
4
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
  • #2
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).
 
  • #3
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.
 
  • #4
True, it is nearly limitless on things to check for to get an accurate word count.
 
  • #5
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();
}
 

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

What is C language?

C language is a high-level, general-purpose programming language originally developed by Dennis Ritchie in the early 1970s. It is commonly used for system programming, embedded systems, and applications where high performance and efficiency are important.

What is the purpose of filing in C language?

Filing in C language allows programmers to read from and write to external files, such as text files, in their programs. This allows for data to be stored and retrieved, making programs more versatile and useful.

How do I open a file in C language?

To open a file in C language, you can use the fopen() function, which takes in the file name and the desired mode (e.g. read, write, append) as parameters. This function returns a file pointer that can be used to read from or write to the file.

What are the different modes for opening a file in C language?

The different modes for opening a file in C language are:

  • r - read mode
  • w - write mode (overwrites existing file or creates a new file)
  • a - append mode (appends to the end of an existing file or creates a new file)
  • r+ - read and write mode
  • w+ - read and write mode (overwrites existing file or creates a new file)
  • a+ - read and write mode (appends to the end of an existing file or creates a new file)

How do I close a file in C language?

To close a file in C language, you can use the fclose() function, which takes in the file pointer as a parameter. It is important to close a file after reading from or writing to it, as it frees up resources and ensures that all data is properly written to the file.

Similar threads

  • Programming and Computer Science
Replies
1
Views
906
  • Programming and Computer Science
Replies
1
Views
648
  • Programming and Computer Science
2
Replies
35
Views
833
  • Programming and Computer Science
Replies
12
Views
2K
  • Programming and Computer Science
Replies
1
Views
362
  • Programming and Computer Science
Replies
33
Views
2K
  • Programming and Computer Science
Replies
2
Views
1K
  • Programming and Computer Science
3
Replies
81
Views
5K
  • Programming and Computer Science
Replies
2
Views
393
  • Programming and Computer Science
Replies
7
Views
2K
Back
Top