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...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in...
C++ #include<iostream> #include<string> #include<fstream> #include<cstdlib> using namespace std; const int ROWS = 8; //for rows in airplane const int COLS = 4; void menu(); //displays options void displaySeats(char[][COLS]); void reserveSeat(char [ROWS][COLS]); int main() { int number=0; //holder variable char seatChar[ROWS][COLS]; for (int i = 0; i < ROWS; i++) { for (int j = 0; j < COLS; j++) { seatChar[i][j] = '-'; } } int choice; //input from menu bool repeat = true; //needed for switch loop while (repeat...
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....
Please program in JAVA: Program each option such as delete line and such. Your program will...
Please program in JAVA: Program each option such as delete line and such. Your program will be a line editor. A line editor is an editor where all operations are performed by entering commands at the command line. Commands include displaying lines, inserting text, editing lines, cutting and pasting text, loading and saving files. For example, a session where the user enters three lines of text and saves them as a new file may appear as: Menu: m Delete line:...
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:        ...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications:...
C++ Pig Latin Lab This assignment uses pointers to perform something done often in computer applications: the parsing of text to find “words” (i.e., strings delineated by some delimiter). Write a program that encodes English language phrases into Pig Latin. Pig Latin is a form of coded language often used for amusement. Many variations exist in the methods used to form Pig Latin phrases. Use the following algorithm: to form a Pig Latin phrase from an English language phrase, tokenize...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return...
C++ 1. Modify the code from your HW2 as follows: Your triangle functions will now return a string object. This string will contain the identification of the triangle as one of the following (with a potential prefix of the word “Right ”): Not a triangle Scalene triangle Isosceles triangle Equilateral triangle 2. All output to cout will be moved from the triangle functions to the main function. 3. The triangle functions are still responsible for rearranging the values such that...
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...