Program:
#include<stdio.h>
int main(){
FILE *fp;
char ch;
int Nl=0,Nw=0,Nc=0;
fp=fopen("abc.txt","r"); //fopen("file PATH","MODE")
//abc.txt...if it is present in same directory of Program.
//OR you can use file path.
if(fp){
while ((ch=getc(fp)) != EOF) {
//EOF-End Of File
//In C/C++, getc() returns EOF when end of file is reached.
if (ch != ' ' && ch != '\n')
Nc++;
if (ch == ' ' || ch == '\n')
Nw++;
if (ch == '\n')
Nl++;
}
if(Nc>0){
Nl++;
Nw++;
}
}
else{
printf("File Can't Open!....try again.\n");
}
printf("\n\tResult:\n");
printf("-->> No. of Characters= %d\n",Nc );
printf("-->> No. of Words= %d\n",Nw );
printf("-->> No. of Lines= %d\n",Nl );
fclose(fp);
}
Expected O/P:
Result:
-->> No. of Characters= 151
-->> No. of Words= 27
-->> No. of Lines= 3
Source file-> abc.txt:
“Believe in yourself! Have faith in your abilities! Without a humble but
reasonable confidence in your own powers you cannot be successful or happy.”
—Norman Vincent Peale
0 Comments
Post a Comment