Question

Description The word bank system maintains all words in a text file named words.txt. Each line...

Description

The word bank system maintains all words in a text file named words.txt. Each line in the text file stores a word while all words are kept in an ascending order. You may assume that the word length is less than 20.

The system should support the following three functions:

  • Word lookup: to check whether a given word exists in the word bank.
  • Word insertion: to insert a new word into the word bank. No insertion should be made if the word already appears in the word bank.
  • Word deletion: to delete a word from the word bank.

Please also make sure that

  • After insertion, the order of words in the text file should be kept in ascending order.
  • After deletion, there should not be any empty lines in the text file.
  • After deleting the last word from the system, the system should still keep an empty words.txt.

Implementation

  • Start your coding with the skeleton code and create a text file words.txt next to the C++ file.
  • You don't need to change the function headers.
  • Complete three core functions lookup, insert_word and delete_word.

Expected Output

Below is a sample run of the program. Assume that the file words.txt is empty initially.

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2
Please enter the word you want to insert: cat
The word "cat" is inserted successfully!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2
Please enter the word you want to insert: apple
The word "apple" is inserted successfully!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2
Please enter the word you want to insert: dog
The word "dog" is inserted successfully!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1
Please enter the word you want to search for: car
The word "car" is not found!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1
Please enter the word you want to search for: cat
The word "cat" is found!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2
Please enter the word you want to insert: cat
The word "cat" already exists!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 3
Please enter the word you want to delete: cat
The word "cat" is deleted successfully!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 3
Please enter the word you want to delete: car
The word "car" is not in the file!
        

Then, below is the expected content of the text file words.txt.

apple
dog
        

Hints

For word insertion and deletion tasks, creating a temporary file may simplify lots of work. This approach is particularly useful if the word bank is large such that you cannot read the whole word bank into memory. For the lab tasks, there are two useful functions for file operation that you may use:

  • rename for renaming a file
  • remove for deleting a file

In addition, we read and manipulate words through char array. The following two functions could be helpful:

  • strcmp for comparing two strings
  • strcpy for copying a string from one char array to another

======================Skeleton Code================================

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>

using namespace std;

#define N 20


// search for a word in the input file, return true if found
bool lookup(const char filename[], const char word[]){
        return false;
}


// delete a word in the input file
// the result should not contain blank lines
void delete_word(const char filename[], const char word[]){
}


// insert a word in the input file such that the words are in ascending order
// it should not insert duplicate word
void insert_word(const char filename[], const char word[]){
}


int main(){

        const char filename[] = "words.txt";
        char choice;
        char word[N];

        while (true){
                cout << "1 for lookup; 2 for insertion; 3 for deletion; q for quit: ";
                cin >> choice;

                if (choice == '1'){
                        cout << "Please enter the word you want to search for: ";
                        cin >> word;
                        if (lookup(filename, word)){
                                cout << "The word \"" << word << "\" is found!" << endl;
                        }else{
                                cout << "The word \"" << word << "\" is not found!" << endl;
                        }
                }else if (choice == '2'){
                        cout << "Please enter the word you want to insert: ";
                        cin >> word;
                        insert_word(filename, word);
                }else if (choice == '3'){
                        cout << "Please enter the word you want to delete: ";
                        cin >> word;
                        delete_word(filename, word);
                }else if (choice == 'q'){
                        break;
                }else{
                        cout << "Invalid input. Please input again." << endl;
                }
                cout << endl;
        }

        return 0;
}

Homework Answers

Answer #1

#include <iostream>
#include <fstream>
#include <string.h>
#include <stdio.h>
using namespace std;

#define N 20

// search for a word in the input file, return true if found
bool lookup(const char filename[], const char word[]){
char word2[20];
ifstream f;
int count=0;
f.open(filename);
while(f>>word2)
{
if(!(strcmp(word2,word)))
count++;
}
if(count>0)
return true;
else
return false;

}


// delete a word in the input file
// the result should not contain blank lines
void delete_word(const char filename[], const char word[]){
char word2[20];
ifstream f;
ofstream f2;
f.open(filename);
f2.open("copy.tmp");
while(f>>word2)
{
if(strcmp(word,word2)!=0)
{
f2<<word2<<endl;
}
}
remove("words.txt");
rename("copy.tmp","words.txt");
}


// insert a word in the input file such that the words are in ascending order
// it should not insert duplicate word
void insert_word(const char filename[], const char word[]){
char word2[20];
int count=0,flag=0;
ifstream f;
ofstream f2;
f.open(filename);
f2.open("copy.tmp");
while(f>>word2)
{
if(strcmp(word2, word)<0 && flag==0)
           {
               f2<<word2<<endl;
               count++;
           }
       else if(strcmp(word2, word)>0 && flag==0 && count==0)
       {
       f2<<word<<endl<<word2<<endl;
       flag=1;
       count++;
      
       }
       else if(strcmp(word2, word)>0 && flag==0 && count>0)
       {
       f2<<word<<endl<<word2<<endl;
       flag=1;
       count++;
       }
       else if(strcmp(word2, word)==0 && flag==0)
       {
       f2<<word2<<endl<<word<<endl;
       flag=1;
       count++;
       }
       else if(flag==1)
       {
       f2<<word2<<endl;
       count++;
       }
  
          
}
   if(flag==0)
       {
       f2<<word<<endl;
       flag=1;
       count++;

       }
remove("words.txt");
rename("copy.tmp","words.txt");
}


int main(){

const char filename[] = "words.txt";
char choice;
char word[N];

while (true){
cout << "1 for lookup; 2 for insertion; 3 for deletion; q for quit: ";
cin >> choice;

if (choice == '1'){
cout << "Please enter the word you want to search for: ";
cin >> word;
if (lookup(filename, word)){
cout << "The word \"" << word << "\" is found!" << endl;
}else{
cout << "The word \"" << word << "\" is not found!" << endl;
}
}else if (choice == '2'){
cout << "Please enter the word you want to insert: ";
cin >> word;
insert_word(filename, word);
}else if (choice == '3'){
cout << "Please enter the word you want to delete: ";
cin >> word;
delete_word(filename, word);
}else if (choice == 'q'){
break;
}else{
cout << "Invalid input. Please input again." << endl;
}
cout << endl;
}

return 0;
}

OUTPUT

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1
Please enter the word you want to search for: happy
The word "happy" is found!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 2
Please enter the word you want to insert: working

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 3
Please enter the word you want to delete: happy

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1
Please enter the word you want to search for: happy
The word "happy" is not found!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: 1
Please enter the word you want to search for: working
The word "working" is found!

1 for lookup; 2 for insertion; 3 for deletion; q for quit: q

Know the answer?
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for?
Ask your own homework help question
Similar Questions
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the...
Can someone please edit my code so that it satisfies the assignments' requirements? I pasted the codes below. Requirement: Goals for This Project:  Using class to model Abstract Data Type  OOP-Data Encapsulation You are asked to write an app to keep track of a relatively small music library. The app should load song information from a data file once the app is started. It should allow user to view, add, remove, and search for songs. The app should...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor...
Programming Exercise 2: implement the member function moveToNth(...) that removes the item marked by the cursor and inserts it as the nth element of the list; test your implementation by turning the flag LAB3_TEST2 from 0 to 1 in config.h; - Programming Exercise 3: implement the ListArray member function find(...) that searches for the element given as a parameter; the search starts at the cursor and stops when it finds the element or at the end of the list; the...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write()...
Lab 6    -   Program #2   -   Write one number to a text file. Use the write() and read() functions with binary                                                        data, where the data is not char type.              (Typecasting is required) Fill in the blanks, then enter the code and run the program. Note:   The data is int type, so typecasting is            required in the write() and read() functions. #include <iostream> #include <fstream> using namespace std; int main() {    const int SIZE = 10;   ...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any...
Write a template-based class that implements a template-based implementation of Homework 3 that allows for any type dynamic arrays (replace string by the template in all instances below). • The class should have: – A private member variable called dynamicArray that references a dynamic array of type string. – A private member variable called size that holds the number of entries in the array. – A default constructor that sets the dynamic array to NULL and sets size to 0....
Writing a program in Python that reads a text file and organizes the words in the...
Writing a program in Python that reads a text file and organizes the words in the file into a list without repeating words and in all lowercase. Here is what I have #This program takes a user input file name and returns each word in a list #and how many different words are in the program. while True:   #While loop to loop program     words = 0     #list1 = ['Programmers','add','an','operating','system','and','set','of','applications','to','the','hardware',          # 'we','end','up','with','a','Personal','Digital','Assistant','that','is','quite','helpful','capable',           #'helping','us','do','many','different','things']        try:        ...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing...
I'm currently stuck on Level 3 for the following assignment. When passing my program through testing associated with the assignment it is failing one part of testing.   Below is the test that fails: Failed test 4: differences in output arguments: -c input data: a b c -c expected stdout: b observed stdout: a b expected stderr: observed stderr: ./test: invalid option -- 'c' Unsure where I have gone wrong. MUST BE WRITTEN IN C++ Task Level 1: Basic operation Complete...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code...
Complete a Java program named ARMgr that maintains customer accounts receivable in a database. The code to initialize the CustomerAccountsDB database table and add a set of customer accounts is provided. Finish the code in these 3 methods in CustomerAccountDB.java to update or query the database: -purchase(double amountOfPurchase) -payment(double amountOfPayment) -getCustomerName() Hint: For getCustomerName(), look at the getAccountBalance() method to see an example of querying data from the database. For the purchase() and payment() methods, look at the addCustomerAccount() method...
Write a program of wordSearch puzzle that use the following text file as an input. The...
Write a program of wordSearch puzzle that use the following text file as an input. The output should be like this: PIXEL found (left) at (0,9). ( Use JAVA Array ) .Please do not use arrylist and the likes! Hints • The puzzle can be represented as a right-sized two-dimensional array of characters (char). • A String can be converted into a right-sized array of characters via the String method toCharArray. . A word can occur in any of 8...
ADVERTISEMENT
Need Online Homework Help?

Get Answers For Free
Most questions answered within 1 hours.

Ask a Question
ADVERTISEMENT