Pushok

Finding digits in string and sorting

Apr 24th, 2011
478
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 0.87 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4.  
  5. int sort(const void* a, const void* b)
  6. {
  7.     return (isdigit(*(char*)a)  && isdigit(*(char*)b) ) ? *(char*)a > *(char*)b : 0;
  8. }
  9.  
  10. int main()
  11. {
  12.     char* arr = (char*) malloc (sizeof(char) * 1024);
  13.     if(arr != 0)
  14.     {
  15.         printf("Input the string:");
  16.         gets(arr);
  17.  
  18.         if(arr != 0)
  19.         {
  20.             char* str = (char*)malloc(strlen(arr) * sizeof(char));
  21.             char* c[2] = {arr, str};
  22.  
  23.             do
  24.             {
  25.                 if(isdigit(*c[0]))
  26.                 {
  27.                     *(c[1]++) = *c[0] ;
  28.                 }
  29.             }while(*((c[0])++) != 0);
  30.  
  31.            *(c[1]++) = 0;
  32.  
  33.             qsort((void*)str, strlen(str), sizeof(char), sort);
  34.  
  35.             printf("%s", str);
  36.             free(str);
  37.         }
  38.     }
  39.  
  40.     free(arr);
  41.     return 0;
  42. }
Advertisement
Add Comment
Please, Sign In to add comment