Question

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

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 exits. The function has no input, but should return a file pointer if the file exits. 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 exits, 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.

Solution Preview

This material may consist of step-by-step explanations on how to solve a problem or examples of proper writing, including the use of citations, references, bibliographies, and formatting. This material is made available for the sole purpose of studying and learning - misuse is strictly forbidden.

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

FILE* openFile();
void wordSearch(FILE *fp);

int main()
{
    FILE* fp = openFile(); //Open the file
    wordSearch(fp); //Test the file
    return 0;
}

FILE* openFile()
{
    FILE* fp; //Declare file pointer
    char fileName[20]; //Filename string (set to 20 but can be anything)
    printf("Please enter a file name(no more than 20 characters): "); //Prompt user for file name
    scanf("%s", fileName); //Read file name

    fp = fopen(fileName, "r"); //Open using string for filename in read mode
    if(fp == NULL) //If the file does not exist
    {
   printf("File does not exist, exiting...\n"); //Tell user and quit
   exit(0);
    }
    return fp; //Return pointer to file
}...

This is only a preview of the solution. Please use the purchase button to see the entire solution

Related Homework Solutions

Malloc
Homework Solution
$8.00
Malloc
Computer
Science
C-Family
Programming
Implementation
Procedures
Memory
Pointer
List
Call
The Simplistic Primary Predictor in C++
Homework Solution
$20.00
Computer Science
Programming
C++
Primary Predictor
Election Results
Factors
Candidates
States
Information
Commercials
Campaign
Calculations
Statements
Variables
Input
Output
Attributes & Methods in C++
Homework Solution
$10.00
Programming
C++
Computer Science
Attributes
Methods
Students
People
Inheritance
Employees
Salaries
Job Titles
Classes
Statements
Variables
Get help from a qualified tutor
Live Chats