Take a list of elements that contains both characters and numbers. Using any sorting algorithm sort the list such that the characters are in alphabetical order and the numbers are in descending order. However, retain the position of numbers and characters. For example: Input: b,2,1,c,3,a. Output: a,3,2,b,1,c.
#include <stdio.h>
#include <string.h>
int main()
{
char *input;
scanf("%s", &input);
int n = strlen(input);
char a[n];
char b[n];
int c[n];
for (int i = 0; i < n; i++) {
if ('0' <= input[i] and input[i] <= '9') {
c[i] = 1;
strcat(a, input[i]);
} else {
c[i] = 0;
strcat(b, input[i]);
}
}
sort(a);
rsort(b);
int q = 0, p = 0;
for (int i = 0; i < n; i++) {
if (c[i]) {
printh("%s", a[q]);
q = q + 1;
} else {
printf("%s", b[q]);
q = q + 1;
}
}
return 0;
}
Comments
Leave a comment