Answered step by step
Verified Expert Solution
Link Copied!

Question

1 Approved Answer

I need help with C program about Cstrings and Recursion. Objective: The purpose of this assignment is to introduce you to the manipulation of cstrings

I need help with C program about Cstrings and Recursion.

image text in transcribedimage text in transcribed

image text in transcribed

Objective:

The purpose of this assignment is to introduce you to the manipulation of cstrings and use of recursion. In the end, you may realize that it is better using iteration instead of recursion for this problem, but for practicing purpose we'll do a recursion anyway.

Description:

You are to develop a program that will search for a word as well as count the number of characters in a text file. Specifically, it will ask the user to input a file name (cstring) and a word (cstring). Then, it will decide whether the word exists in the file, and count the number of characters (A-Z lower or upper case alphabets, excluding all other symbols or special characters) in the file. After that, it will report the results: whether the word is contained in the file and how many characters are in the file.

Instructions:

Here are some basic components for your program design.

First, your main program gets the file name (string) from the keyboard input, make sure that the file exists while opening the file. Specifically, your program asks "Please input the text file name:" Then it uses scanf to read the string from the keyboard, which is the file name for fopen. If the file does not exist, which means that fopen will return NULL instead of the pointer to the file, your program notify the user and quit.

Second, if the file exists, the main program asks for a word "Please input the word to search:". After receiving the word from the keyboard, it calls search_and_count() to search the word and count the characters in the file. So search_and_count() will have the file pointer and the word as input, and an indicator (0 or 1 -- whether the word is contained in the file) and the total number of characters ('A'-'Z' or 'a'-'z' that are in the file) as output. Your main program then reports whether the word exists in the file, and how many characters are in the file.

search_and_count() will read a string from the file. If the reading of a string ends up with EOF return, the reading is at the end of the file, the program returns. Otherwise, it counts the characters in the string, compares the word with the string (to see if the word exists in the file), and calls itself to read the next string from the file until the end of the file as described earlier, which is the exit condition of the recursion. You may need to build two more functions:

search_and_count() reads a string from the file into a variable; when this is successful, it calls function character_count() to count the number of characters in the string, and then it calls function word_search() to compare the word with the string to decide whether it is a match. If there is a match already, it doesn't need the comaprison any more. This will go on in a recursive call until the end of file. In other words, word_search() will be called only if your program hasn't found a match.

character_count() should have an input, which is the current string, and a return value, which is the count of the number of alphabetical characters in the string.

word_search() should have two input, the word and the string, and a return value 0 or 1 as an indicator whether the word is the same as the string. The program should allow the string has an ending punctuation. That is, you may want to put a NULL in the string to make it the same length as the word, so that you can use strcmp to compare the word with the string. You don't need to consider that the word is a substring of the compared string.

Finally, in the main program, close your file and report whether there is a match and how many characters are in the file.

Objective: The purpose of this assignment is to introduce you to the manipulation of cstrings and use of recursion. In the end, you may realize that it is better using iteration instead of recursion for this problem, but for practicing purpose we'll do a recursion anyway

Step by Step Solution

There are 3 Steps involved in it

Step: 1

blur-text-image

Get Instant Access to Expert-Tailored Solutions

See step-by-step solutions with expert insights and AI powered tools for academic success

Step: 2

blur-text-image

Step: 3

blur-text-image

Ace Your Homework with AI

Get the answers you need in no time with our AI-driven, step-by-step assistance

Get Started

Recommended Textbook for

Machine Learning And Knowledge Discovery In Databases European Conference Ecml Pkdd 2010 Barcelona Spain September 2010 Proceedings Part 2 Lnai 6322

Authors: Jose L. Balcazar ,Francesco Bonchi ,Aristides Gionis ,Michele Sebag

2010th Edition

364215882X, 978-3642158827

More Books

Students also viewed these Databases questions