-
Notifications
You must be signed in to change notification settings - Fork 0
/
sortingwords.c
86 lines (83 loc) · 2.63 KB
/
sortingwords.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
#include <stdio.h>
#include <ctype.h>
#include <string.h>
int firstcompare(char first_word1, char first_word2) { //a>B.. first>second= 1...second>first=0...first=second=2..
char litter1 = first_word1; char litter2 = first_word2;
first_word1 = tolower(first_word1);
first_word2 = tolower(first_word2);
if (first_word1 > first_word2) {
return 1;
}
else if (first_word1 < first_word2) {
return 0;
}
else {
if (litter1 > litter2) {
return 1;
}
else if (litter1 < litter2) {
return 0;
}
else return 2;
}
}
int numbericcompare(char first_word1, char first_word2) {
if (first_word1 > first_word2) {
return 1;
}
else if (first_word1 < first_word2) {
return 0;
}
else return 2;
}
void copy(char* tothis, char* thiis) {
for (int i = 0; thiis[i] != 0 || thiis[i + 1] != 0||thiis[i+2]!=0; i++) {
tothis[i] = thiis[i];
}
}
int main(void) {
char words[1000][1006] = { 0 }; int counter = 0;
for (int i = 0;; i++) {
scanf("%s", &words[i]);
if (words[i][0] == '0') {
break;
}
int l = strlen(words[i]);
counter++;
words[i][l + 2] = counter % 100;
words[i][l + 1] = counter / 100;
}
for (int i = 0; i < counter-1; i++) {
char lowest[1000] = { 0 }; copy(lowest, words[i]); int temp = i;
for (int j = i + 1; j < counter; j++) {
int a = firstcompare(lowest[0], words[j][0]) ;
if (a==1) {
copy(lowest, words[j]); temp = j;
}
if (a == 2) {
if (words[j][1] == 0) {
copy(lowest, words[j]); temp = j;
}
else {
a = firstcompare(lowest[1], words[j][1]);
if (a == 1) {
copy(lowest, words[j]); temp = j;
}
if (a == 2) {
int l1 = strlen(lowest); int l2 = strlen(words[j]);
a = numbericcompare(lowest[l1 + 1], words[j][l2 + 1]);
if(a==2)a= numbericcompare(lowest[l1 + 2], words[j][l2 + 2]);
if (a == 1) {
copy(lowest, words[j]); temp = j;
}
}
}
}
}
copy(words[temp], words[i]);
copy(words[i], lowest);
}
for (int i = 0; i < counter; i++) {
printf("%s ", words[i]);
}
}