analyzeFile(); accepts two parameters: the path to a file as a string, and anoth
ID: 3821617 • Letter: A
Question
analyzeFile(); accepts two parameters: the path to a file as a string, and another string ("upper", "lower", digit", or "space"); open the file, then determine how many characters of the chosen type are in the file, then close the file; make the number available to the calling interface otherwise. analyzeFile() should call one of the following functions: countUpper (): return the number of uppercase letters in the file countLower(): return the number of lowercase letters in the file countDigits(): return the number of digits in the file countSpaces(): return the number of whitespace characters (including tabs but not line breaks) found in the file countWords(): return the number of whitespace characters (including tabs) found in the file isLower(): accepts a character as a parameter; return true if it is a lowercase letter and false otherwise isUpper(): accepts a character as a parameter; return true if it is an uppercase letter and false otherwise isDigit(): accepts a character as a parameter return true if it is a digit and false otherwise Use these functions to create a program that: Continuously prompts the user to enter: the path to a file as a string the type of character to be searched as a string When the file is successfully opened and a valid character type has been chosen: count the instances of the selected character type in the file Display the results Sample Run 1 (Inputs will be shaded. Bold outputs must change depending on the input.) Enter the absolute path to a file. S:mbrown_sharedInput_Fileslab6-5.txt File opened successfully; what type of character should be counted? lowercase There are 93 lowercase letters in the file.Explanation / Answer
Code:-
#include <stdio.h>
#include <string.h>
int analyzefile(char file_name2[25],char type_s[10]){ //analyse chooses what type of character user seeks and accordingly a particular function is called
int count=0;
if(strcmp(type_s,"upper")){ //if uppercase characters count required
count=countUpper(file_name2);
}
if(strcmp(type_s,"lower")){ //if lowercase characters count required
count=countLower(file_name2);
}
if(strcmp(type_s,"digit")){ //if digits count required
count=countDigit(file_name2);
}
if(strcmp(type_s,"space")){ //if no of spaces count required
count=countSpace(file_name2);
}
return count;
}
int countUpper(char file_name_U[25]){
FILE *fp; //file pointer
int count_U=0;
char ch_U;
fp = fopen(file_name_U,"r"); // read mode
while( ( ch_U = fgetc(fp) ) != EOF ){ //contents of file is read untill the last element
if (ch_U >= 65 && ch_U <= 90){ //condition for uppercase
count_U++; //count increased if condition is approved
}
}
fclose(fp); //close the file
return count_U; //return the count
}
int countLower(char file_name_L[25]){
FILE *fp;
int count_L=0;
char ch_L;
fp = fopen(file_name_L,"r"); // read mode
while( ( ch_L = fgetc(fp) ) != EOF ){ //contents of file is read untill the last element
if (!(ch_L >= 65 && ch_L <= 90)){ //condition for lowercase
count_L++;//count increased if condition is approved
}
}
fclose(fp); //close the file
return count_L; //return the count
}
int countDigit(char file_name_D[25]){
FILE *fp;
int count_D=0;
char ch_D;
fp = fopen(file_name_D,"r"); // read mode
while( ( ch_D = fgetc(fp) ) != EOF ){ //contents of file is read untill the last element
if (isdigit(ch_D)){//condition for digits
count_D++;//count increased if condition is approved
}
}
fclose(fp); //close the file
return count_D; //return the count
}
int countSpace(char file_name_S[25]){
FILE *fp;
int count_S=0;
char ch_S;
fp = fopen(file_name_S,"r"); // read mode
while( ( ch_S = fgetc(fp) ) != EOF ){ //contents of file is read untill the last element
if (ch_S == ' '){//condition for spaces
count_S++;//count increased if condition is approved
}
}
fclose(fp); //close the file
return count_S; //return the count
}
int main()
{
char str[10], file_name[25];
printf("Enter the absolute path of the file : ");
gets(file_name);
printf("Type of characters you want to count : ");
gets(str);
int number;
number=analyzefile(file_name,str);
printf(" the no of the selected type of characters are :- %d",number);
return 0;
}
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.