generated from threeal/project-starter
-
Notifications
You must be signed in to change notification settings - Fork 1
/
solution.c
116 lines (111 loc) · 2.17 KB
/
solution.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
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
// The solution can be done by counting the number of vowel occurrences and then replacing each vowel using that count as references.
// This enum helps store the count of vowels in an array.
enum {
A = 0,
E,
I,
O,
U,
a,
e,
i,
o,
u
};
char* sortVowels(char* s) {
int vowelsCount[] = {0, 0, 0, 0, 0, 0, 0, 0, 0, 0};
// Count the vowels from the given string.
char* c = s;
while (*c != 0) {
// Switch case is used for faster lookup.
switch (*c) {
case 'A':
++vowelsCount[A];
break;
case 'E':
++vowelsCount[E];
break;
case 'I':
++vowelsCount[I];
break;
case 'O':
++vowelsCount[O];
break;
case 'U':
++vowelsCount[U];
break;
case 'a':
++vowelsCount[a];
break;
case 'e':
++vowelsCount[e];
break;
case 'i':
++vowelsCount[i];
break;
case 'o':
++vowelsCount[o];
break;
case 'u':
++vowelsCount[u];
break;
}
++c;
}
// From the first vowel, replace each vowel in the given string according to the vowel count.
int vowel = A;
c = s;
while (*c != 0) {
// Switch case is used for faster lookup.
switch (*c) {
case 'A':
case 'E':
case 'I':
case 'O':
case 'U':
case 'a':
case 'e':
case 'i':
case 'o':
case 'u':
while (vowelsCount[vowel] <= 0) ++vowel;
switch (vowel) {
case A:
*c = 'A';
break;
case E:
*c = 'E';
break;
case I:
*c = 'I';
break;
case O:
*c = 'O';
break;
case U:
*c = 'U';
break;
case a:
*c = 'a';
break;
case e:
*c = 'e';
break;
case i:
*c = 'i';
break;
case o:
*c = 'o';
break;
case u:
*c = 'u';
break;
}
--vowelsCount[vowel];
break;
}
++c;
}
// Return the modified string.
return s;
}