-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy path339A.c
48 lines (38 loc) · 1.16 KB
/
339A.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
#include <stdio.h>
int main(){
char str[101], numStr[51];
// Input string here
scanf("%s", str);
getchar();
// Get the numbers from the string and push it into other string
int index = 0, pushIdx = 0;
while (str[index] != '\0') {
if (str[index] >= '1' && str[index] <= '3') {
numStr[pushIdx] = str[index];
pushIdx++;
}
index++;
}
numStr[pushIdx] = '\0'; // Set null char in the end of the string
index--;
// Sorting number char algorithm here
for (; index >= 0; index--) {
if (index % 2 == 0) {
// Search for the maximum number in the string
int counter = 0, maxIdx = 0;
while (numStr[counter] != '\0') {
if (numStr[maxIdx] < numStr[counter]) {
maxIdx = counter;
}
counter++;
}
// Set current element with the max value
str[index] = numStr[maxIdx];
numStr[maxIdx] = '0'; // Replace the max value's index in numStr with '0'
}
}
// Print the output
printf("%s\n", str);
getchar();
return 0;
}