Code:
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<conio.h>
#include<ctype.h>
void Keyword(FILE*fptr,int); /* Keyword function Prototype*/
void Long_Word(FILE*,char); /* Longest Word function Prototype*/
void N_sentence(FILE*,int); /* Sentence function Prototype*/
void N_Quotations(FILE*,int); /* Double Quotes function Prototype*/
void N_Vowels(FILE*,int); /* Vowel function Prototype*/
void N_Zeros(FILE*,int); /* Zero function Prototype*/
int main () /*MAIN PROGRAM*/
{
char keyword [25]; /*holds the keyword to be searched*/
char kw [25];
int lw [25];
int ns=0;
int nq=0;
int nv=0;
int nz=0;
char filename [30]; /*stores the filename of the file to be open*/
printf("Enter Filename: ");
scanf("%s",filename);
FILE *fptr; /*file pointer to fptr*/
fptr = fopen(filename,"r"); /*opens file and reads from it*/
if (fptr == NULL) /*if file is empty do not open*/
{
printf("File Cannot Be Open.\n");
printf("Please Try Again\a\a");
printf("\n\n\n");
system ("cls");
system("pause");
printf("Enter Filename:");
scanf("%s",filename);
}
kw=Keyword(*fptr); /* Function Call*/
lw=Long_Word(); /* Function Call*/
ns=N_sentence(); /* Function Call*/
nq=N_Quotations(); /* Function Call*/
nv=N_Vowels(); /* Function Call*/
nz=N_Zeros(); /*Function Call*/
fclose(fptr); /*CLOSE FILE POINTER*/
return 0;
}
/*****************************************************************************************/
/*Search Keyword Function*/
void Keyword(FILE*fptr,int k_count)
{
char key_temp [25]; /*holds each string in the text file to be compared with keyword*/
int k_word=0; /*k_word is used as a counter and is initialized to zero*/
int i; /*i checks each character*/
char keyword [25]; /*keyword enter by user*/
/*fptr file pointer*/
fptr=fopen("lamming.txt","r"); /*opens file and reads from it*/
printf("Enter A Single keyword:");
scanf("%s",keyword);
fscanf(fptr,"%s",key_temp);
while(!feof(fptr))
{
for(i=0; i<=24; i++)
{
key_temp[i]=tolower(key_temp[i]);
}
if(strcmp(keyword,key_temp)==0)
{
k_word++;
}
fscanf(fptr,"%s",key_temp);
}
printf("The Keyword %s Occurs %d time(s).",keyword,k_word);
fclose(fptr);
}
/*****************************************************************************************/
/*The longest word function*/
void Long_Word(FILE *Fptr,char long_word[])
{
char longest_word [25]; /*longest word */
char lword [25];
int length=0;
int lw_counter=0;
fptr=fopen("lammin.txt","r");
fscanf(fptr,"%s",longest_word);
length=strlen(longest_word);
strcpy(lword,longest_word);
while(!feof(fptr))
{
if (strlen(longest_word)>strlen(lword))
{
length=strlen(lword);
strcpy(lword,longest_word);
}
fscanf(fptr,"%s",longest_word);
}
printf("The Longest Word In The File Is ""%s"" " ,lword);
fclose(fptr);
}
/********************************************************************************/
/*Function that counts the number of sentences within the file*/
void N_sentence(FILE *fptr,int nsent)
{
char fullstop ='.';
char numb_sentences;
int s_count=0;
fptr=fopen("lammin.txt","r");
while(!feof(fptr))
{
fscanf(fptr,"%c",&numb_sentences);
if (numb_sentences == fullstop)
{
s_count++;
}
}
printf("There are %d sentences in this file.",s_count);
fclose(fptr);
}
/******************************************************************************/
/*Function counts the number of double quotation marks*/
void N_Quotations(FILE *fptr,int quotes)
{
fptr=fopen("lammin.txt","r");
char numb_quotes; /*numb_quotes searches the file for quotes*/
char n_quotes ='"'; /*n_quotes is a constant*/
int quotes=0; /*quotes reps. the counter*/
while(!feof(fptr))
{
fscanf(fptr,"%c",&numb_quotes);
if(numb_quotes == n_quotes)
{
quotes++;
printf("%c",numb_quotes);
}
}
printf("The Estimated Number Of Double Quotation Marks Are %d \n",quotes);
fclose(fptr);
}
/*********************************************************************************/
/*The function counts the frequencies of all the vowels found in the file*/
void N_Vowels(FILE *fptr,int vowels)
{
fptr=fopen("lammin.txt","r");
char vowels; /*vowels checks each character in the file*/
int v_a=0; /*counter for vowel 'a'*/
int v_e=0; /*counter for vowel 'e'*/
int v_i=0; /*counter for vowel ''*/
int v_o=0; /*counter for vowel 'o'*/
int v_u=0; /*counter for vowel 'u'*/
while(!feof(fptr))
{
fscanf(fptr,"%c",&vowels);
if(vowels == 'a'|| vowels == 'A')
v_a++;
if(vowels == 'e'|| vowels =='E')
v_e++;
if(vowels == 'i'|| vowels =='I')
v_i++;
if(vowels == 'o'|| vowels == 'O')
v_o++;
if(vowels == 'u'|| vowels == 'U')
v_u++;
}
printf("The vowel A occurs %d times in the file.\n",v_a);
printf("The vowel E occurs %d times in the file.\n",v_e);
printf("The vowel I occurs %d times in the file.\n",v_i);
printf("The vowel O occurs %d times in the file.\n",v_o);
printf("The vowel U occurs %d times in the file.\n",v_u);
fclose(fptr);
}
/********************************************************************************/
/*Function Searches the file for the number of zeros and prints the amount found*/
void N_Zeros(FILE *fptr,int zeros)
{
char zeros; /*zero checks each character for zero n the file*/
int z=0;
fptr=fopen("lammin.txt","r");
while(!feof(fptr))
{
fscanf(fptr,"%c",&zeros);
if (zeros == '0')
{
z++;
}
}
printf("The Number Zero Occurs In The File %d Times.\n",z);
fclose(fptr);
}
/**************************************************************************************/