Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

THIS IS A C PROGRAMMING HOMEWORK PROBLEM. PLEASE SHOW ALL CODE AND FOLLOW THE IN

ID: 642442 • Letter: T

Question

THIS IS A C PROGRAMMING HOMEWORK PROBLEM. PLEASE SHOW ALL CODE AND FOLLOW THE INSTRUCTIONS BELOW. THANKS!

Objective:

The purpose of this assignment is to introduce you to the manipulation of arrays and cstrings. You must use at least one cstring function to complete this assignment.

Description:

You are to develop a program that will search for a word as well as count the number of words and characters in a text file. Specifically, it will ask the user to input a file name and a word (string) to search. Then it will decide whether the string exists, and count the number of characters (A-Z lower or upper case alphabets, excluding all other symbols or special characters) and words in the file. To simplify the matter, you can consider any character string that is more than 2 characters separated by space, return, or TAB as a word. After that, you report the statistics to the user.


Instructions:

Here are some basic components for your program design.

First, you have a function to get the file name, open the file, and make sure that the file exists. The function has no input, but should return a file pointer if the file exists. The program asks "Please input the text file name:" Then it reads the input into a cstring, which can be used as a file name to open the file. If the file does not exist, your program notify the user and quit.

Second, if the file exists, your program calls another function that asks for a word "Please input the word to search:". The function will read a string, process the string, and repeat until the end of the file. The input to this function is the file pointer, and the output is a match indicator, number of words, and number of characters.

This function can read a string from the file into a variable, increase the word count, call another function to count the characters of the string, add the count into the total number of characters, and call another function to compare the string with the "word" to decide whether it is a match. This will go on in a loop until the end of file.

The function that counts the number of characters should have an input, which is the current string, and an output, which is the count of the number of characters in the string; the function that compares the word with the current string should return yes or no. Presumably it should answer yes even if the word is a substring of the current string.

Finally, you report whether there is a match and how many words and characters are in the file.

Explanation / Answer

#include<stdio.h>
#include <string.h>
#include<unistd.h>

int NUM_OF_CHARACTERS = 0;
int NUM_OF_WORDS = 0;

int characters(char s[]){

int len = 0;
while(s[len++] != '');

return len-1;
}

int matches(char word[], char search[]){

int i = 0 ;
int len1 = characters(word);
int len2 = characters(search);

if( len1 != len2) return 0;

while(len1--){

if(word[len1] != search[len1]) return 0;
}

return 1;

}

void function(FILE *f){
printf("Please input the word to search: ");
char search[100];
scanf("%s", search);

char word[200];
int i = 0 ;
int flag = 0 ;
char ch;
while((ch=getc(f))!=EOF){

if(ch != ' '&& ch != ' ' && ch != ' '){
word[i++] = ch;
}
else
{
  
word[i] = '';
NUM_OF_CHARACTERS += characters(word);
NUM_OF_WORDS += 1;
  
flag = flag | matches(word, search);

i = 0;
}
}

word[i] = '';
  
NUM_OF_CHARACTERS += characters(word);
NUM_OF_WORDS += 1;
flag = flag | matches(word, search);

printf("Is Word Present in file?: ");
if( flag) printf("YES ");
else
printf("NO ");
printf("NUM_OF_WORDS: %d ", NUM_OF_WORDS);
printf("NUM_OF_CHARACTERS: %d ", NUM_OF_CHARACTERS);


}

int main()
{
FILE *f;
char ch;
printf("Please input the text file name: ");
char fileName[100];
scanf("%s", fileName);
f=fopen(fileName,"r");

if( access( fileName, R_OK ) != -1 ) {
// file exists
function(f);
} else {
printf("file doesn't exist");
}

fclose(f);

return 0;
}