Advertisement
Guest User

Untitled

a guest
Nov 26th, 2015
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 1.94 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <string.h>
  3. #include <stdlib.h>
  4.  
  5. int dlinaSlova(int i,char *str)
  6. {
  7.   int k, len=0;
  8.   k=i;
  9.   while(str[k] != ' ' && str[k] != '\0') {
  10.     len++;
  11.     k++;
  12.   }
  13.   if (str[k]=='\0') len--;
  14.   return len;
  15. }
  16.  
  17. int nextIndex(int i,char* str)
  18. {
  19.   int j,l=i;
  20.   if (str[i]==' ') {
  21.     while (str[l]==' ') l++;
  22.   }
  23.   j=dlinaSlova(l,str) + l;
  24.   while (str[j]==' ')
  25.     j++;
  26.   return j;
  27. }
  28.  
  29. int nel(char* str)
  30. {
  31.   int k=0,i=0,x=strlen(str)-1,flag;
  32.   while (x>0) {
  33.     x=x+i-nextIndex(i, str);
  34.     k++;
  35.     i= nextIndex(i, str);
  36.    }
  37.   return k;
  38. }
  39.  
  40. char csort(char *src, char *dest)
  41. {
  42.     int **s = calloc(2,sizeof(int*));
  43.     int *ns = calloc(nel(src),sizeof(int));
  44.     int i,j,l=0;
  45.     while (src[l]==' ') l++;
  46.     int k=l;
  47.     s[1] = calloc(nel(src), sizeof(int));
  48.     s[0] = calloc(nel(src), sizeof(int));
  49.     int **count = calloc(2*nel(src),sizeof(int*));
  50.     count[1] = calloc(nel(src), sizeof(int));
  51.     count[0] = calloc(nel(src), sizeof(int));
  52.     for (j=0;j<nel(src);j++) {
  53.       s[0][j]=dlinaSlova(l,src);
  54.       l=nextIndex(l,src);
  55.     }
  56.     l=k;
  57.     for (j=0;j<nel(src);j++) {
  58.       s[1][j]=l;
  59.       l=nextIndex(l,src);
  60.      }
  61.     for (j=0;j<(nel(src)-1);j++) {
  62.       i=j+1;
  63.       while (i<nel(src)) {
  64.     if (s[0][i]<s[0][j]) count[1][j]++;
  65.     else count[1][i]++;
  66.     i++;
  67.       }
  68.     }
  69.    
  70.     for (j=0;j<nel(src);j++)
  71.     count[0][j]=s[1][j];
  72.     for (i=0;i<nel(src);i++)
  73.       ns[count[1][i]]=count[0][i];
  74.     k=0;
  75.     for (i=0;i<nel(src);i++) {
  76.       for (j=0;j<dlinaSlova(ns[i],src);j++,k++) dest[k]=src[ns[i]+j];
  77.       dest[k+1]=' ';
  78.       k+=2;
  79.     }
  80.     dest[k-1]='$';
  81. }
  82.  
  83. char main()
  84. {
  85.   int i;
  86.   char* predloz= (char*)malloc(100 * sizeof(char));
  87.   fgets(predloz,100,stdin);
  88.   char *vuvod = calloc(100 , sizeof(char));    
  89.   csort(predloz,vuvod);
  90.   i=0;
  91.   while ((vuvod[i]!='$')&&(i<100)) {
  92.     printf("%c",vuvod[i]);
  93.     i++;
  94.   }
  95.   free(predloz);
  96.   free(vuvod);
  97. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement