//Exercise 3: Write a C Program to count the number of characters, words, lines, spaces and tabs in the input supplied from a file.
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
#include<stdio.h>
void header()
{
printf("\n\n\n\n\n\n\n\t\tBUILT AND DESIGNED BY >> ARSLAN UD DIN SHAFIQ\n\n\t\tWebsite>>\twww.CWorldbyAS.blogspot.com\n");
printf("\n\t\tCOMSATS Institute of Information Technology,\n\n\t\t\t\t\t Lahore , Pakistan\n\n\n");
}
int main()
{
FILE *f,*e;
char a[500],ch;
int chr=0,sp=0,words=0,lines=0,tabs=0;
header();
system("pause");
system("cls");
f=fopen("abc.txt" , "w");
if(f==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
printf("\nWrite on File or CTRL+Z to exit : ");
gets(a);
while(!feof(stdin))
{
fprintf(f,"%s\n",a);
gets(a);
}
fclose(f);
e=fopen("abc.txt" , "r");
if(e==NULL)
{
printf("\nUnable to Open File...!!");
return 1;
}
rewind(e);
while((ch=fgetc(e))!=EOF)
{
if (ch>='A' || ch>='a' && ch<='z' || ch<='Z')
{
chr++;
}
if(ch==' ')
{
sp++;
words++;
}
if(ch=='\t')
{
tabs++;
words++;
}
if(ch=='\n')
{
lines++;
words++;
}
}
printf("\nCharacters : %d\nSpaces : %d \nWords : %d\nLines : %d\nTabs : %d ",chr,sp,words,lines,tabs);
}
No comments:
Post a Comment