Ad

Chapter - 13: File Input-Output

Write a program to carry out the following:


Ad

- To read a text file “TRIAL.TXT” consisting of a maximum of 50 lines of text, each line with a maximum of 80 characters.
- Count and display the number of words contained in the file.
- Display the total number of four-letter words in the text file.

Assume that the end of a word may be a space, comma or a full-stop followed by one or more spaces or a newline character.


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

#define EndOfWord word[i] == ' ' || word[i] == ','\
 || word[i] == '.' || word[i] == '\n'

int count_word(FILE*);
void count_4_words(FILE*);

int main()
{
	FILE *fp;
	fp = fopen("File (p).txt", "r");
	if (fp == NULL)
	{
		puts("\nCan't open the file.");
		exit(1);
	}

	int count = count_word(fp);
	printf("\nThere are total %d words in file.\n", count);
	_getch();
	system("cls");
	count_4_words(fp);
	_getch();
	return 0;
}

int count_word(FILE *fp)
{
	int i, count = 0;
	char word[20];
	for (i = 0; 1; i++)
	{
		word[i] = fgetc(fp);
		if (word[i] == EOF)
			break;

		/*A word ends if a space or comma or etc. encounter.*/
		if (EndOfWord)
		{

			/*If the 'word' variable contain only one letter and viz.
			'.' or ',' or ' ' or '\n' so it skips it as 
			it is not count as a word*/
			if (i != 0)		
				count++;

			i = -1;
		}
	}
	return count;
	fclose(fp);
}

void count_4_words(FILE *fp)
{
	int i, count = 0;
	char word[20];
	if (fp == NULL)
	{
		puts("\nCan't open the file.");
		exit(1);
	}
	puts("\nTotal number of four letter words are following.\n");
	for (i = 0; 1; i++)
	{
		word[i] = fgetc(fp);
		if (word[i] == EOF)
			break;

		/*A word ends if a space or comma or etc. encounter.*/
		if (EndOfWord)
		{
			/*If the 'word' variable contain only one letter and viz.
			'.' or ',' or ' ' or '\n' so it skips it as
			it is not count as a word*/
			if (i != 0)		
			{
				if (i == 4)
				{
					/*Terminating the word before printing it*/
					word[i + 1] = '\0';
					puts(word);	
				}
			}
			i = -1; 
		}
	}
	fclose(fp);
}
Ad

© 2024 Garbage Valuegarbage value logo