/*
Title: b) Find out number of characters, words, spaces and sentences from a file and write result in another file.
*/

#include<stdio.h>
#include<stdlib.h>

int main(int argc,char *argv[])
{
FILE *fp,*fp1;
char x;
int nol=0,now=0,nosp=0,noc=0,spf=1;
system("clear");
if(argc!=2)
{
printf("\nWrong no of arguemnts");
getchar();
exit(0);
}

fp=fopen(argv[1],"r");
if(fp==NULL)
{
printf("\nFile opening error:");
exit(0);
}
while((x=fgetc(fp))!=EOF)
{
if(x=='\n')
nol++;
if(isspace(x))
{
if(x!='\n')
nosp++;
spf=1;
}
if(isalnum(x) && spf==1)
{
now++;
spf=0;
}
if(!isspace(x))
noc++;
}
fclose(fp);

if(noc>0)
{
fp1=fopen(argv[2],"w");
fprintf(fp1,"NO of Lines=%d",nol);
fprintf(fp1,"\nNO of Words=%d",now);
fprintf(fp1,"\nNO of Spaces=%d",nosp);
fprintf(fp1,"\nNO of Chars=%d\n",noc);
fprintf(fp1,"\n File created succesfully\n");
getchar();
//fclose(fp1);
}
else
printf("\nFile is empty");

return 0;
}



//END OF PROGRAM