Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include<iostream>
- using namespace std;
- int length(const char *a)
- {
- int i = 0;
- while (a[i])
- i++;
- return i;
- }
- bool isLetter(const char c)
- {
- if ((c >= 'A' && c <= 'Z') || (c >= 'a' && c <= 'z'))
- return true;
- return false;
- }
- int wordCount(const char *a)
- {
- int i = 0, temp = 0, count = 0;
- while (i < length(a))
- {
- if (isLetter(a[i]))
- temp++;
- else if (temp>0)
- {
- temp = 0;
- count++;
- }
- i++;
- }
- if (temp > 0)
- count++;
- return count;
- }
- void copy(char *a, const char *b)
- {
- int i;
- for (i = 0; i < length(b); i++)
- a[i] = b[i];
- a[i] = '\0';
- }
- void copy2(char *a, const char *b)
- {
- a[0] = (char)(b[0] + 32);
- int i;
- for (i = 1; i < length(b); i++)
- {
- a[i] = b[i];
- }
- a[i] = '\0';
- }
- int compareLex(const char *a, const char *b)
- {
- int i = 0, check = 0;
- char *tempA = new char[length(a) + 1];
- char *tempB = new char[length(b) + 1];
- if (a[i] >= 'A' && a[i] <= 'Z')
- {
- copy2(tempA, a); check++;
- }
- if (b[i] >= 'A' && b[i] <= 'Z')
- {
- copy2(tempB, b);
- if (check == 0)check = 2;
- else if (check == 1)check = 3;
- }
- if (check == 0)
- {
- while (a[i] && b[i] && a[i] == b[i])
- i++;
- delete[] tempA;
- delete[] tempB;
- return a[i] - b[i];
- }
- else if (check == 1)
- {
- while (tempA[i] && b[i] && tempA[i] == b[i])
- i++;
- delete[] tempB;
- return tempA[i] - b[i];
- }
- else if (check == 2)
- {
- while (a[i] && tempB[i] && a[i] == tempB[i])
- i++;
- delete[] tempA;
- return a[i] - tempB[i];
- }
- else
- {
- while (tempA[i] && tempB[i] && tempA[i] == tempB[i])
- i++;
- return tempA[i] - tempB[i];
- }
- }
- void sort(char **arr, int count)
- {
- int maxIndex = 0;
- char *max = new char[500];
- for (int i = 0; i < count - 1; i++)
- {
- copy(max, arr[i]);
- maxIndex = i;
- for (int j = i + 1; j < count; j++)
- {
- if (compareLex(max, arr[j]) < 0)
- {
- copy(max, arr[j]);
- maxIndex = j;
- }
- }
- copy(arr[maxIndex], arr[i]);
- copy(arr[i], max);
- }
- }
- void print(char **arr, int n)
- {
- for (int i = 0; i < n - 1; i++)
- {
- int check = 0;
- for (int j = i + 1; j < n; j++)
- {
- if (compareLex(arr[i], arr[j]) == 0)
- {
- check++;
- }
- }
- if(check==0)
- {
- cout << arr[i] << endl;
- }
- if (i == n - 2)cout << arr[i + 1] << endl;
- }
- }
- int main()
- {
- int n;
- do
- {
- cin >> n;
- } while (n < 1 || n>500);
- cin.ignore();
- char *text = new char[n + 2];
- cin.getline(text, n + 1);
- int wCount = wordCount(text);
- char **arr = new char*[wCount];
- for (int i = 0; i < wCount; i++)
- {
- arr[i] = new char[n];
- }
- int indexA = 0, indexT = 0;
- char *temp = new char[n]; int indexTemp = 0;
- while (indexT < n)
- {
- while (isLetter(text[indexT]))
- {
- temp[indexTemp] = text[indexT];
- indexTemp++;
- indexT++;
- }
- temp[indexTemp] = '\0';
- copy(arr[indexA], temp);
- indexA++;
- indexT++;
- indexTemp = 0;
- }
- sort(arr, wCount);
- print(arr, wCount);
- system("pause");
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement