$(document).ready(function() { $('pre code').each(function(i, block) { hljs.highlightBlock(block); }); });

Sunday, July 11, 2010

Character counting program

#include
#include
#include
void main()
{
FILE *fp;
int count_space=0,count_chars=0,count_tab=0,count_line=0;
char name[20],fname[20],str;
clrscr();
printf("\nEnter the name of text file to be created:\n");
gets(name);
strcpy(fname,name);
strcat(fname,".txt");

if((fp=fopen(fname,"w"))==NULL)
{
printf("\n\tSorry, file could not be created.");
printf("\n\tProgram will exit now.");
delay(2000);
exit(0);
}
else
{
printf("\nA file with name '%s' has been created.",fname);
printf("Now enter the string please.");
printf("\n(Press 'Esc' to quit entering string):\n");
do
{
if((str=getch())!='\r')
{
printf("%c",str);
if(str==' ')
str='-';
}
else
{
printf("\n");
str='|';
}
putc(str,fp);
}while(str!=27);
///Esc key
fclose(fp);
}
fp=fopen(fname,"r");
if(fp==NULL)
{
printf("\nSorry, File Read Error.The program will exit now.");
delay(2000);
exit(0);
}
else
{
while(!feof(fp))
{
str=getc(fp);
count_chars++;
if(str=='-')
count_space++;
else if(str=='\t')
count_tab++;
else if(str=='|')
count_line++;
}
count_chars-=count_space;
count_chars-=count_tab;
count_chars-=count_line;
count_chars-=2;
printf("\n\t-------------------------------");
printf("\n\tDetails");
printf("\n\t-------------------------------");
printf("\n\tFilename: %s",fname);
printf("\n\tNo. of Characters in file: %d characters (alphabets+other characters),",count_chars);
printf("\n\tNo. of Spaces in file: %d spaces,",count_space);
printf("\n\tNo. of Tabs in file: %d tabs,",count_tab);
printf("\n\tNo. of Newlines in file: %d newlines.",count_line);
}
getch();
}

No comments:

Post a Comment

Contact Me

Name

Email *

Message *