Answer to Question #244519 in C for Smiley006

Question #244519

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.


1
Expert's answer
2021-09-29T18:44:16-0400
#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;
}

Need a fast expert's response?

Submit order

and get a quick answer at the best price

for any assignment or question with DETAILED EXPLANATIONS!

Comments

No comments. Be the first!

Leave a comment

LATEST TUTORIALS
APPROVED BY CLIENTS