/*write a c program to count the nmuber of character, number of word, and
number of lines from a text file and display the result*/
#include<stdio.h>
#include<conio.h>
void main()
{
FILE *fp;
char ch;
int nol=0,now=0,noc=0;
clrscr();
fp=fopen("c.txt","r");
while(1)
{
ch=fgetc(fp);
if(ch==EOF)
break;
else
noc++;
if((ch==' ')||(ch=='\n'))
now++;
if(ch=='\n')
nol++;
}
printf("\nno. of word are:%d",now);
printf("\nno. of character are:%d",noc);
printf("\nno. of lines are:%d",nol);
fclose(fp);
getch();
}
/*
no. of word are:4
no. of character are:13
no. of lines are:3
Type EXIT to return to Turbo C++. . .Microsoft(R) Windows DOS
(C)Copyright Microsoft Corp 1990-2001.
C:\TC\BIN>type c.txt
sd df
erw
ey
C:\TC\BIN>
C:\TC\BIN>exit
*/
No comments:
Post a Comment