Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <iostream>
- #include <cstring>
- #include <string.h>
- using namespace std;
- enum choices { ADD, DELETEWORD, SEARCHWORD, PRINTCHAR, PRINTALL, EXIT };
- char * searchStr(char** lex, int size, char word[])
- {
- for (char**p = lex; p < lex + size; p++)
- {
- if (strcmp(*p, word) == 0)
- return *p;
- if (strcmp(*p, word) > 0)
- return NULL;
- }
- return NULL;
- }
- void newStr(char **&lex, int &size, char word[])
- {
- char**help = new char*[size + 1];
- int i = 0;
- if (size == 0)
- {
- help[i] = new char[strlen(word) + 1];
- strcpy_s(help[i], strlen(word) + 1, word);
- delete[]lex;
- lex = help;
- size++;
- }
- while (strcmp(lex[i], word) < 0)
- {
- help[i] = lex[i];
- i++;
- }
- help[i] = new char[strlen(word) + 1];
- strcpy_s(help[i], strlen(word) + 1, word);
- while (i < size)
- {
- help[i + 1] = lex[i];
- i++;
- }
- delete[] lex;
- lex = help;
- size++;
- }
- void printAll(char**lex, int &size)
- {
- for (int i = 0; i < size; i++)
- cout << lex[i] << " ";
- }
- void delStr(char**&lex, int&size, char word[])
- {
- char**help = new char*[size - 1];
- int i = 0;
- if (searchStr(lex, size, word) == NULL)
- return;
- while (strcmp(lex[i], (char*)word) != 0)
- {
- help[i] = lex[i];
- i++;
- }
- help[i] = lex[i + 1];
- i++;
- while (i < size - 1)
- {
- help[i] = lex[i + 1];
- i++;
- }
- delete[]lex;
- lex = help;
- size--;
- }
- void printChar(char**lex, int size, char tav)
- {
- int j = 0;
- for (int i = 0; i < size; i++)
- {
- if (lex[i][0] == tav)
- cout << lex[i] << " ";
- }
- cout << endl;
- }
- int main()
- {
- int size = 0;
- char **lexicon = new char*[1];
- char Word[80];
- char tavprint;
- bool run = true;
- int choice;
- do
- {
- cout << "enter 0-5:" << endl;
- cin >> choice;
- switch (choice)
- {
- case ADD:
- cout << "enter the word" << endl;
- cin >> Word;
- newStr(lexicon, size, Word);
- printAll(lexicon, size);
- break;
- case DELETEWORD:
- cout << "enter the word to delete:" << endl;
- cin >> Word;
- delStr(lexicon, size, Word);
- printAll(lexicon, size);
- break;
- case SEARCHWORD:
- cout << "enter the word to search for:" << endl;
- cin >> Word;
- if (searchStr(lexicon, size, Word) == NULL)
- {
- cout << "not found" << endl;
- }
- else
- {
- cout << "found" << endl;
- }
- break;
- case PRINTCHAR:
- cout << "enter the char:" << endl;
- cin >> tavprint;
- printChar(lexicon, size, tavprint);
- break;
- case PRINTALL:
- printAll(lexicon, size);
- break;
- case EXIT:
- return 0;
- default:
- cout << "ERROR" << endl;
- break;
- }
- } while (run = true);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement