CSE 365 Arizona State University C Programing Dictionary Application Code

Question Description

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.txtdictionary.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.txtdictionary.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 Engdictionary.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.$
$ ./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 0dictionary.txt has 3 words.$
$ ./dict -p Eng -n 1dictionary.txt has 3 words.English: the language of England, widely used in many varieties throughout the world.$
$ ./dict -p Eng -n 2dictionary.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 Frenchdictionary.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 ardictionary.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 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.

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.

Grading

Out of 100 points, you will get 30 points if your program compiles and runs. For the other 70 points, you will get 10 points for each test case that your submission passes. Each test case will test only one (1) feature. Therefore, your best strategy is to ensure that your program compiles and runs, then implement the features one by one.

Note that the point of this homework assignment is for you to learn C or C++. Do not worry too much about nasty corner cases.

Special policy

For this assignment, you may use any existing programs or program snippets that you can find online, as long as you clearly specify in comments of your program where the code is coming from. Here is a good example:

/* This function is adapted from https://stackoverflow.com/questions/NNNNNNNN/xxxxx */int lookup(char* word){    char local_word[32] = {0};    strcpy(local_word, word);

Place your order
(550 words)

Approximate price: $22

Calculate the price of your order

550 words
We'll send you the first draft for approval by September 11, 2018 at 10:52 AM
Total price:
$26
The price is based on these factors:
Academic level
Number of pages
Urgency
Basic features
  • Free title page and bibliography
  • Unlimited revisions
  • Plagiarism-free guarantee
  • Money-back guarantee
  • 24/7 support
On-demand options
  • Writer’s samples
  • Part-by-part delivery
  • Overnight delivery
  • Copies of used sources
  • Expert Proofreading
Paper format
  • 275 words per page
  • 12 pt Arial/Times New Roman
  • Double line spacing
  • Any citation style (APA, MLA, Chicago/Turabian, Harvard)

Our guarantees

Delivering a high-quality product at a reasonable price is not enough anymore.
That’s why we have developed 5 beneficial guarantees that will make your experience with our service enjoyable, easy, and safe.

Money-back guarantee

You have to be 100% sure of the quality of your product to give a money-back guarantee. This describes us perfectly. Make sure that this guarantee is totally transparent.

Read more

Zero-plagiarism guarantee

Each paper is composed from scratch, according to your instructions. It is then checked by our plagiarism-detection software. There is no gap where plagiarism could squeeze in.

Read more

Free-revision policy

Thanks to our free revisions, there is no way for you to be unsatisfied. We will work on your paper until you are completely happy with the result.

Read more

Privacy policy

Your email is safe, as we store it according to international data protection rules. Your bank details are secure, as we use only reliable payment systems.

Read more

Fair-cooperation guarantee

By sending us your money, you buy the service we provide. Check out our terms and conditions if you prefer business talks to be laid out in official language.

Read more