Implement a dictionary application Solved

45.00 $

Categories: ,

Description

5/5 - (3 votes)

Introduction

Implement a dictionary application using C or C++ with the following features:

 

Load a dictionary file.

 

Given a prefix string that the user specifies, print the first word or all words in the dictionary with that string as their prefix.

 

Given two strings A and B that the user specifies, replace all occurrences of A in the dictionary file with B.

 

Spawning a new editor (e.g., vim) to allow the user to modify the dictionary file. Save the dictionary file afterwards.

 

You must use either C or C++ to implement your dictionary application. All your source code must be put within one (1) file. You will only submit that one file.

 

Your program should not have any extra library dependencies.

 

A set of test cases are provided as examples throughout this document. You may test your program against the test cases and make sure the output is the same. However, when grading, your program will be tested against a different set of test cases that will not be available to you.

 

Do not obfuscate your program.

 

Details

You are supposed to implement a dictionary application that interacts with users through CLI (command line interface). Each feature and its parameters are specified by command line arguments. Your program should print the output to stdout unless specified otherwise.

 

Environment

Your submission will be compiled under Ubuntu 20.04 with the following command line:

 

$ gcc dict.c -g -O1 -o dict or

 

$ g++ dict.cpp -g -O1 -o dict

Then the generated dict file will be tested under Ubuntu 20.04 against seven (7) test cases.

 

Feature A: Loading a dictionary file

A dictionary file is a text file where each line has a word, a colon as a separator, and the definition of the word itself. You may assume all lines are separated by a \n only. Here is an example dictionary file dictionary.txt with three words and their definitions.

 

English: the language of England, widely used in many varieties throughout the world. muggle: a person who is not conversant with a particular activity or skill. hacker: a person or thing that hacks or cuts roughly.

When loading a dictionary file, you may safely ignore all lines that do not follow the above format. But please note that there may or may not be a space right after the colon separator – I put a space after colons just to make the text more readable.

 

Throughout this document, we will assume that the executable of your application is called dict. The user may specify a dictionary file to load by the “-d” switch in command line.

 

$ ./dict -d dictionary.txt dictionary.txt has 3 words.

$

If -d is not provided or the file that is specified does not exist, your program should fall back to dictionary.txt. You may assume that dictionary.txt always exists under the same directory of your application dict. You may also assume that the user always runs dict from the directory where it is stored.

 

$ ./dict  # no dictionary file name is provided. fall back to dictionary.txt dictionary.txt has 3 words.

$

Since grading is automated for this homework assignment, you want to ensure that your application prints the exact same output as in the above examples. You will lose points if the output has misspelled words, l33tspeak, etc. Be careful 🙂

 

Feature B: Prefix searching

A dictionary is useless if the user cannot search words (or their meanings) in it. So your application must support prefix-based searches.

 

A user may specify a prefix string using -p, and your application should print out all the words and their meanings if these words start with the given prefix. Here is an example:

 

$ ./dict -d dictionary.txt -p Eng dictionary.txt has 3 words.

English: the language of England, widely used in many varieties throughout the world.

$

$ ./dict -d dictionary.txt -p muggg  # no word start with “muggg” dictionary.txt has 3 words.

$

Prefix matching is case-sensitive, which means the prefix “eng” should not match against word “English”:

 

$ ./dict -d dictionary.txt -p eng  # no word start with “eng” dictionary.txt has 3 words.

$

$ ./dict -p ng  # no word start with “ng” dictionary.txt has 3 words.

$

The user may also specify the maximum number of results that should be printed out by using the -n switch. Here is an example.

 

$ ./dict -p Eng -n 0 dictionary.txt has 3 words.

$

$ ./dict -p Eng -n 1 dictionary.txt has 3 words.

English: the language of England, widely used in many varieties throughout the world.

$

$ ./dict -p Eng -n 2 dictionary.txt has 3 words.

English: the language of England, widely used in many varieties throughout the world.

$

Feature C: Word replacement

With the help of programs, it is a lot easier to fix typos or spelling mistakes in batch!

 

A user may specify a word to search for using -s and another word to replace it with throughout the dictionary file using -r. The dictionary file should be updated as the final result. Nothing needs to be printed out to stdout during searching and replacing.

 

You may assume -s and -r are always specified at the same time. If -r is provided without -s (or vice versa), your program may gracefully quit without doing anything.

 

Here is an example.

 

$ ./dict -s English -r French dictionary.txt has 3 words.

$

After running the above command, the dictionary.txt will look like this:

 

French: the language of England, widely used in many varieties throughout the world. muggle: a person who is not conversant with a particular activity or skill.

hacker: a person or thing that hacks or cuts roughly.

Here is another example where the user wants to replace a word with two words.

 

$ ./dict -s English -r “l33t speak” dictionary.txt has 3 words.

$

After running the above command, the dictionary.txt will look like this:

 

l33t speak: the language of England, widely used in many varieties throughout the world.

muggle: a person who is not conversant with a particular activity or skill. hacker: a person or thing that hacks or cuts roughly.

The user may also replace any substring of a word to something else, such as in the following example:

 

$ ./dict -s rson -r ar dictionary.txt has 3 words.

$

After running the above command, the dictionary.txt will look like this:

 

English: the language of England, widely used in many varieties throughout the world.

muggle: a pear who is not conversant with a particular activity or skill. hacker: a pear or thing that hacks or cuts roughly.

Feature D: Spawning an editor

“Search and replace” is too powerful sometimes. We want to allow our beloved users to manually edit the dictionary file!

 

The user may use the -v switch to specify the full path to an editor that they like. Your application should open the editor for the user so that the user can use the editor to edit the dictionary file. Note that you can assume whatever the editor is specified, the editor will take the first argument that is after its path as the file to open.

 

$ ./dict -v /bin/nano dictionary.txt has 3 words.

# Then nano opens with the content of dictionary.txt displayed inside

This feature is probably the trickiest to implement among all since you need to worry a little about security: Assuming your application dict is an SUID-root executable, we do not want users to be able to pass in rm -rf / –no-preserve-root as the editor path and totally wipes the hard drive.

 

Therefore, here are some important points that you may want to consider:

 

You may want to implement some sanity checks or filtering mechanisms for the editor that the user specifies to ensure users will not do rm -rf / –no-preserve-root.

 

You may want to drop privileges (switch from the effective uid to real uid) before launching the editor.

 

Remember that for this assignment, security comes after feature completeness. Your submission will only be graded based on its features, not on its security. However, I do like to see if you can implement some security features, such as filtering of certain arguments.

 

Submission

All your source code must be put within one (1) file called dict.c or dict.cpp. You will then submit that one file to the submission system. No other files will be taken.

 

Your program should not have any extra library dependencies. Do not obfuscate your program.

  • dict-9i3r08.zip