Wednesday 28 September 2011


COUNT THE NUMBER OF CHARACTERS

Aim:

       
To write a C program that counts the number of characters and number of lines in a
file.

Algorithm:
1. Start the program.
2. Open the file in the read mode.
3. Get each character from the file the end of file.
4. Increment the counter variable for the character and number of lines is determined.
5. While the end of file is encountered, the loop gets terminated. The number of character and lines are displayed.
6. End the program.

Program:

#include <stdio.h>
void main()
FILE *fopen( ),*fp;
intch,nc,nlines;
char fname [30];
nlines = 0;
nc = 0;
printf(”enter file name”);
fflish(stdin(;
scanf(”%s”,fname));
fp=fopen(fname,”r”);
if(fp= = NULL)
printf(”cannot open the file %s for reading”,fname);
exit(0);
}
ch=getc(fp);
while(ch ! =EOF)
}

Output :
Enter file name
A.txt
There are 7 character in A.txt
There are 2 lines.

Result:
Thus the Program is written, executed and output is tested.

0 comments:

Post a Comment