Advertisement
Guest User

Untitled

a guest
Jan 27th, 2020
79
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.56 KB | None | 0 0
  1. #include <iostream>
  2. #include <cstring>
  3. #include <string.h>
  4. using namespace std;
  5. enum choices { ADD, DELETEWORD, SEARCHWORD, PRINTCHAR, PRINTALL, EXIT };
  6. char * searchStr(char** lex, int size, char word[])
  7. {
  8. for (char**p = lex; p < lex + size; p++)
  9. {
  10. if (strcmp(*p, word) == 0)
  11. return *p;
  12. if (strcmp(*p, word) > 0)
  13. return NULL;
  14. }
  15. return NULL;
  16. }
  17. void newStr(char **&lex, int &size, char word[])
  18. {
  19. char**help = new char*[size + 1];
  20. int i = 0;
  21. if (size == 0)
  22. {
  23. help[i] = new char[strlen(word) + 1];
  24. strcpy_s(help[i], strlen(word) + 1, word);
  25. delete[]lex;
  26. lex = help;
  27. size++;
  28. }
  29. while (strcmp(lex[i], word) < 0)
  30. {
  31. help[i] = lex[i];
  32. i++;
  33. }
  34. help[i] = new char[strlen(word) + 1];
  35. strcpy_s(help[i], strlen(word) + 1, word);
  36. while (i < size)
  37. {
  38. help[i + 1] = lex[i];
  39. i++;
  40. }
  41. delete[] lex;
  42. lex = help;
  43. size++;
  44. }
  45. void printAll(char**lex, int &size)
  46. {
  47. for (int i = 0; i < size; i++)
  48. cout << lex[i] << " ";
  49. }
  50. void delStr(char**&lex, int&size, char word[])
  51. {
  52. char**help = new char*[size - 1];
  53. int i = 0;
  54. if (searchStr(lex, size, word) == NULL)
  55. return;
  56. while (strcmp(lex[i], (char*)word) != 0)
  57. {
  58. help[i] = lex[i];
  59. i++;
  60. }
  61. help[i] = lex[i + 1];
  62. i++;
  63. while (i < size - 1)
  64. {
  65. help[i] = lex[i + 1];
  66. i++;
  67. }
  68. delete[]lex;
  69. lex = help;
  70. size--;
  71. }
  72. void printChar(char**lex, int size, char tav)
  73. {
  74. int j = 0;
  75. for (int i = 0; i < size; i++)
  76. {
  77. if (lex[i][0] == tav)
  78. cout << lex[i] << " ";
  79. }
  80. cout << endl;
  81. }
  82. int main()
  83. {
  84. int size = 0;
  85. char **lexicon = new char*[1];
  86. char Word[80];
  87. char tavprint;
  88. bool run = true;
  89. int choice;
  90. do
  91. {
  92. cout << "enter 0-5:" << endl;
  93. cin >> choice;
  94. switch (choice)
  95. {
  96. case ADD:
  97. cout << "enter the word" << endl;
  98. cin >> Word;
  99. newStr(lexicon, size, Word);
  100. printAll(lexicon, size);
  101. break;
  102. case DELETEWORD:
  103. cout << "enter the word to delete:" << endl;
  104. cin >> Word;
  105. delStr(lexicon, size, Word);
  106. printAll(lexicon, size);
  107. break;
  108. case SEARCHWORD:
  109. cout << "enter the word to search for:" << endl;
  110. cin >> Word;
  111. if (searchStr(lexicon, size, Word) == NULL)
  112. {
  113. cout << "not found" << endl;
  114. }
  115. else
  116. {
  117. cout << "found" << endl;
  118. }
  119. break;
  120. case PRINTCHAR:
  121. cout << "enter the char:" << endl;
  122. cin >> tavprint;
  123. printChar(lexicon, size, tavprint);
  124. break;
  125. case PRINTALL:
  126. printAll(lexicon, size);
  127. break;
  128. case EXIT:
  129. return 0;
  130. default:
  131. cout << "ERROR" << endl;
  132. break;
  133. }
  134.  
  135. } while (run = true);
  136. return 0;
  137. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement