0% found this document useful (0 votes)
1 views2 pages

C Programming Ex10

This document outlines a C program designed to count the number of characters, words, and lines in a specified file. It includes a step-by-step procedure for implementation and provides the complete code for the program. The results indicate that the program successfully executes its intended function.

Uploaded by

Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
1 views2 pages

C Programming Ex10

This document outlines a C program designed to count the number of characters, words, and lines in a specified file. It includes a step-by-step procedure for implementation and provides the complete code for the program. The results indicate that the program successfully executes its intended function.

Uploaded by

Malathi
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 2

EX.

NO: 10 COUNT THE NUMBER OF CHARACTERS, WORDS, AND


LINES IN A FILE.
DATE:
AIM
To write a C program to count the number of characters, words, and
lines in a file.

PROCEDURE:
Step 1: Start
Step 2: Read the file name
Step 3: Open the given file
Step 4: for each character in the file
characters++;
if the character is newline then
lines++;
if character is ' ' || '\n' || '\t' || '\0' then
words++;
Step 5: Display the total number of characters, words, and lines
Step 6: Stop

PROGRAM:

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

void main()
{
FILE *file;
char filename[100];
char ch;
int characters = 0, words = 0, lines = 0;
clrscr();
printf("Enter the filename: ");
scanf("%s", filename);
file = fopen(filename, "r");
if (file == NULL) {
printf("Could not open file %s\n", filename);
return 1;
}
while ((ch = fgetc(file)) != EOF) {
characters++;
if (ch == '\n' || ch == '\0') {
lines++;
}
if (ch == ' ' || ch == '\n' || ch == '\t' || ch == '\0') {
words++;
}
}

fclose(file);
printf("\n Characters: %d ", characters);
printf("\n Words: %d ", words);
printf("\n Lines: %d ", lines);
getch();
}

RESULTS:
Thus, the C program to count the number of characters, words, and lines
in a file was successfully executed.

You might also like