Please help in filing in c lang.

  • Thread starter Yamna
  • Start date
  • #1
4
0
hi.
please can any one 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:
 

Answers and Replies

  • #2
11
0
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 in to 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
AlephZero
Science Advisor
Homework Helper
7,002
297
4 - Compare character you read in to 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
11
0
True, it is nearly limitless on things to check for to get an accurate word count.
 
  • #5
4
0
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 Threads on Please help in filing in c lang.

  • Last Post
Replies
6
Views
2K
  • Last Post
Replies
10
Views
3K
  • Last Post
Replies
6
Views
3K
Replies
39
Views
1K
Replies
4
Views
3K
Replies
27
Views
2K
Replies
5
Views
2K
Replies
1
Views
4K
Replies
11
Views
32K
Replies
20
Views
3K
Top