Question

In: Computer Science

Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...

Implement a dictionary application using 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.

All your source code must be put within one (1) file and your program should not have any extra library dependencies.

Please provide the coding in C and should compile with "$ gcc dict.c -g -O1 -o dict"

Good example of the file running is "./dict -d dict.txt"

Solutions

Expert Solution

PROGRAM :

SHORT AND EASY WAY TO IMPLEMENT DICT. USING C LANGUAGE :

Step 1:
Create a structure with two char arrays

Key
Value
This structure will act as a dictionary which can store key and value pair.
To make it simple I have considered key and value to be text data

Step 2:
Create a dynamic linked list with the structure created.
i.e. When ever a new key and value is added, add a node with the structure.
When ever the key and value is removed, delete the node.

Step 3:
Create functions for reading, searching based on key and value.
And even sorting the dictionary if required

ALGO USED :

Create a dict
Lets add foo, and bar to the dict
and print their values
Lets delete them
see, their gone, there NULL
add them again to proof it works

#include <stdio.h>
#include <stdlib.h>
#include <string.h>

/ Comment this line out to compile without a main function (used when including into another application). /
#define TEST TRUE

typedef struct dict_t_struct {
char *key;
void *value;
struct dict_t_struct *next;
} dict_t;

dict_t **dictAlloc(void) {
return malloc(sizeof(dict_t));
}

void dictDealloc(dict_t **dict) {
free(dict);
}

void getItem(dict_t dict, char *key) {
dict_t *ptr;
for (ptr = dict; ptr != NULL; ptr = ptr->next) {
if (strcmp(ptr->key, key) == 0) {
return ptr->value;
}
}

return NULL;
}

void delItem(dict_t *dict, char key) {
dict_t ptr, prev;
for (ptr = *dict, prev = NULL; ptr != NULL; prev = ptr, ptr = ptr->next) {
if (strcmp(ptr->key, key) == 0) {
if (ptr->next != NULL) {
if (prev == NULL) {
*dict = ptr->next;
} else {
prev->next = ptr->next;
}
} else if (prev != NULL) {
prev->next = NULL;
} else {
*dict = NULL;
}

free(ptr->key);
free(ptr);

return;
}
}
}

void addItem(dict_t *dict, char key, void *value) {
delItem(dict, key);
dict_t *d = malloc(sizeof(struct dict_t_struct));
d->key = malloc(strlen(key)+1);
strcpy(d->key, key);
d->value = value;
d->next = *dict;
*dict = d;
}

#ifdef TEST

int main(int argc, char **argv) {

dict_t **dict = dictAlloc();

addItem(dict, "foo", "bar");
addItem(dict, "bar", "foo");

printf("%s %s\n", (char )getItem(*dict, "foo"), (char )getItem(*dict, "bar"));

delItem(dict, "foo");
delItem(dict, "bar");

printf("%s %s\n", (char )getItem(*dict, "foo"), (char )getItem(*dict, "bar"));

addItem(dict, "foo", "bar");
addItem(dict, "bar", "foo");
addItem(dict, "bar", "pan");

printf("%s %s\n", (char )getItem(*dict, "foo"), (char )getItem(*dict, "bar"));

delItem(dict, "foo");
delItem(dict, "bar");

dictDealloc(dict);

return 0;
}

#endif


Related Solutions

Implement a dictionary application using C++ with the following features: Load a dictionary file. Given a...
Implement a dictionary application using 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...
Implement the following socket programming in C (b) Chat Application using TCP
Implement the following socket programming in C (b) Chat Application using TCP
c# Create a console application that protects an XML file, such as the following example. Note...
c# Create a console application that protects an XML file, such as the following example. Note that the customer's credit card number and password are currently stored in clear text. The credit card must be encrypted so that it can be decrypted and used later, and the password must be salted and hashed: <?xml version="1.0" encoding="utf-8" ?> <customers> <customer> <name>Bob Smith</name> <creditcard>1234-5678-9012-3456</creditcard> <password>Pa$$w0rd</password> </customer> </customers>
Implement an application that will read data from an input file, interpret the data, and generate...
Implement an application that will read data from an input file, interpret the data, and generate the output to the screen. - The application will have two classes, Rectangle and Lab2ADriver. - Name your Eclipse project, Lab2A. Implement the Rectangle class with the following description. Rectangle Data fields -numRows: int -numCols: int -filled: Boolean Store the number of rows in the Rectangle Store the number of columns in the Rectangle Will define either a filled or unfilled rectangle True =...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary...
The course is Data Structures and am using Javascript and Atom... QUESTION 1. Implement the dictionary data structure using the prototype. Run some tests that show that your code works. 2. Implement the hash table data structure using the prototypeRun some tests that show that your code works. 3. The book discusses linear probing, but their approach has a serious problem. What is the issue? 4. Complete the method below that adds all key-value pairs from one dictionary into another....
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using...
C++ coding question From the text file given to you- “worldpop.txt”, perform the following tasks using Boolean function. PS-*Write separate codes for each task* Task 1. Display the names of the countries with: 1. Population >=1000,000,000 2. Population <= 1000,000 Task 2. Display the names of the first 10 countries Task 3. Display the names of the last 10 countries contents of worldpop.txt: Afghanistan 32738376 Akrotiri 15700 Albania 3619778 Algeria 33769669 Andorra 72413 Angola 12531357 Anguilla 14108 Argentina 40677348 Armenia...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write...
URGENT: USING C++: You are given a main.cpp and a Schedule.h file. You are to write a Schedule.cpp file that implements the missing pieces from Schedule.h. // ************************************ // ************* main.cpp ************ // ************************************ #include "Schedule.h" void main() { Schedule s1(10); s1.addEntry(1,20,"Feed Cat"); s1.addEntry(2,40,"Feed Dog"); s1.addEntry(2,50,"Walk Dog"); s1.addEntry(4,0, "Fix Dinner"); s1.addEntry(5,15,"Eat Dinner"); s1.printSchedule(); Schedule s2(s1); // Note this uses the copy constructor cout << endl << "Output from s2 " << endl; s2.printSchedule(); } // ********************************************** // ************* Schedule.h ******************...
Write a C function to implement operation of a stack using the following format: /** *...
Write a C function to implement operation of a stack using the following format: /** * function: *       push * * expects: *       pointer to the stack *       pointer to the size *       the value to push * * returns: *     true when value has been pushed *       false otherwise * * The push function push a value to the passed in stack */ bool push(int *stack, int *size, int max_size, int to_push) {...
How would I create a nested dictionary given a csv file in Python? Say I want...
How would I create a nested dictionary given a csv file in Python? Say I want to make a dictionary that read {'country':{'China':'Fit', 'China':'Overweight', 'USA': 'Overweight', 'USA': 'Fit', 'England':'Fit'...}, 'category':{'Asian':'Fit', 'Caucasian': 'Overweight', 'Caucasian':'Overweight', 'Asian': 'Fit', 'Middle Eastern': 'Fit'...}} given a file that had country category Weight China Asian Fit China Caucasian Overweight USA Caucasian Overweight USA Asian Fit England Middle Eastern Fit... ... And so on in the file.
Answer the following questions based on the given c file: #include #include #include    int c...
Answer the following questions based on the given c file: #include #include #include    int c = 0; void *fnC() {     int i;     for(i=0;i<10;i++)     {   c++;         printf(" %d", c);     }       } int main() { int rt1, rt2;   pthread_t t1, t2; int trial_count = 0; // For every trial, lets zero out the counter and run the count routine “twice”     // as threads that can be scheduled onto independent cores instead of running     // in sequence. for...
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT