Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <stdio.h>
- #include <string.h>
- void csort(char *src, char *dest) {
- long i = 0, j = 0, k, m, n;
- while (src[i]) {
- while ( src[i] && src[i] == ' ' ){
- i += 1;
- }
- i += 1;
- while ( src[i] && src[i] != ' ' ){
- i += 1;
- }
- while ( src[i] && src[i] == ' '){
- i += 1;
- }
- j += 1;
- }
- unsigned long count[j], l[j];
- i = 0;
- k = 0;
- while (src[i]) {
- while (src[i] && src[i] == ' '){
- i +=1;
- }
- count[k] = i;
- i += 1;
- while (src[i] && src[i] != ' '){
- i += 1;
- }
- l[k] = i - count[k];
- while ( src[i] && src[i] == ' '){
- i += 1;
- }
- k += 1;
- }
- i = j;
- i -= 2;
- while (i >= 0) {
- k = i + 1;
- n = i;
- while ( k < j && k >=0) {
- if (l[k] < l[n]) {
- m = l[k];
- l[k] = l[n];
- l[n] = m;
- m = count[k];
- count[k] = count[n];
- count[n] = m;
- n = k;
- }
- k += 1;
- }
- i -= 1;
- }
- i = 0;
- k = 0;
- while ( i < j ) {
- m = count[i];
- while ( l[i] > 0){
- dest[k] = src[m];
- m += 1;
- k += 1;
- l[i] -= 1;
- }
- dest[k] = ' ';
- i+=1;
- k+=1;
- }
- dest[k - 2 + 1] = 0;
- }
- int main() {
- char src[1000];
- gets(src);
- unsigned long i;
- if(src[0] == ' '){
- i = 1;
- } else {
- i = 2;
- }
- int j = strlen(src) - 2 + 1;
- while (j > 0) {
- if (src[j] == ' ' && src[j - 1] == ' ') {
- j -= 1;
- i += 1;
- while ( j >= 0 && src[j] == ' '){
- j -= 1;
- };
- }
- j -= 1;
- i += 1;
- }
- char dest[i];
- csort(src, dest);
- printf("%s", dest);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement