Advertisement
Guest User

Untitled

a guest
Aug 22nd, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.92 KB | None | 0 0
  1. #include<iostream>
  2.  
  3. using namespace std;
  4.  
  5. int length(const char *a)
  6. {
  7. int i = 0;
  8. while (a[i])
  9. i++;
  10. return i;
  11. }
  12. bool isLetter(const char c)
  13. {
  14. if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
  15. return true;
  16. return false;
  17. }
  18. int wordCount(const char *a)
  19. {
  20. int i = 0, temp = 0, count = 0;
  21. while (i < length(a))
  22. {
  23. if (isLetter(a[i]))
  24. temp++;
  25. else if (temp>0)
  26. {
  27. temp = 0;
  28. count++;
  29. }
  30. i++;
  31. }
  32. if (temp > 0)
  33. count++;
  34. return count;
  35. }
  36. void copy(char *a, const char *b)
  37. {
  38. int i;
  39. for (i = 0; i < length(b); i++)
  40. a[i] = b[i];
  41. a[i] = '\0';
  42. }
  43. void copy2(char *a, const char *b)
  44. {
  45. a[0] = (char)(b[0] + 32);
  46. int i;
  47. for (i = 1; i < length(b); i++)
  48. {
  49. a[i] = b[i];
  50. }
  51. a[i] = '\0';
  52. }
  53. int compareLex(const char *a, const char *b)
  54. {
  55. int i = 0, check = 0;
  56. char *tempA = new char[length(a) + 1];
  57. char *tempB = new char[length(b) + 1];
  58. if (a[i] >= 'A' && a[i] <= 'Z')
  59. {
  60. copy2(tempA, a); check++;
  61. }
  62. if (b[i] >= 'A' && b[i] <= 'Z')
  63. {
  64. copy2(tempB, b);
  65. if (check == 0)check = 2;
  66. else if (check == 1)check = 3;
  67. }
  68. if (check == 0)
  69. {
  70. while (a[i] && b[i] && a[i] == b[i])
  71. i++;
  72. delete[] tempA;
  73. delete[] tempB;
  74. return a[i] - b[i];
  75. }
  76. else if (check == 1)
  77. {
  78. while (tempA[i] && b[i] && tempA[i] == b[i])
  79. i++;
  80. delete[] tempB;
  81. return tempA[i] - b[i];
  82. }
  83. else if (check == 2)
  84. {
  85. while (a[i] && tempB[i] && a[i] == tempB[i])
  86. i++;
  87. delete[] tempA;
  88. return a[i] - tempB[i];
  89. }
  90. else
  91. {
  92. while (tempA[i] && tempB[i] && tempA[i] == tempB[i])
  93. i++;
  94. return tempA[i] - tempB[i];
  95. }
  96. }
  97. void sort(char **arr, int count)
  98. {
  99. int maxIndex = 0;
  100. char *max = new char[500];
  101. for (int i = 0; i < count - 1; i++)
  102. {
  103. copy(max, arr[i]);
  104. maxIndex = i;
  105. for (int j = i + 1; j < count; j++)
  106. {
  107. if (compareLex(max, arr[j]) < 0)
  108. {
  109. copy(max, arr[j]);
  110. maxIndex = j;
  111. }
  112. }
  113. copy(arr[maxIndex], arr[i]);
  114. copy(arr[i], max);
  115. }
  116. }
  117. void print(char **arr, int n)
  118. {
  119. for (int i = 0; i < n - 1; i++)
  120. {
  121. int check = 0;
  122. for (int j = i + 1; j < n; j++)
  123. {
  124. if (compareLex(arr[i], arr[j]) == 0)
  125. {
  126. check++;
  127. }
  128. }
  129. if(check==0)
  130. {
  131. cout << arr[i] << endl;
  132. }
  133. if (i == n - 2)cout << arr[i + 1] << endl;
  134. }
  135. }
  136. int main()
  137. {
  138. int n;
  139. do
  140. {
  141. cin >> n;
  142. } while (n < 1 || n>500);
  143. cin.ignore();
  144. char *text = new char[n + 2];
  145. cin.getline(text, n + 1);
  146. int wCount = wordCount(text);
  147. char **arr = new char*[wCount];
  148. for (int i = 0; i < wCount; i++)
  149. {
  150. arr[i] = new char[n];
  151. }
  152. int indexA = 0, indexT = 0;
  153. char *temp = new char[n]; int indexTemp = 0;
  154. while (indexT < n)
  155. {
  156. while (isLetter(text[indexT]))
  157. {
  158. temp[indexTemp] = text[indexT];
  159. indexTemp++;
  160. indexT++;
  161. }
  162. temp[indexTemp] = '\0';
  163. copy(arr[indexA], temp);
  164. indexA++;
  165. indexT++;
  166. indexTemp = 0;
  167. }
  168. sort(arr, wCount);
  169. print(arr, wCount);
  170. system("pause");
  171. return 0;
  172. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement