Advertisement
Guest User

Untitled

a guest
Nov 28th, 2014
156
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 2.41 KB | None | 0 0
  1. //
  2. //  countsort.c
  3. //  
  4. //
  5. //  Created by MacBook return 0; on 08.11.14.
  6. //
  7. //
  8.  
  9. #include <stdio.h>
  10. #include <stdlib.h>
  11. #include <string.h>
  12.  
  13.  
  14. int wcount(char *s)
  15. {
  16.     int k, count = 0;
  17.     int len=strlen(s);
  18.     for (k = 0; k < len; k++) {
  19.         if (s[k]!=' '){
  20.             count++;
  21.             do k++; while ((k < len) && (s[k]!=' ') && (s[k]!='\0') && (s[k]!='\t'));
  22.         }
  23.     }
  24.     return count;
  25. }
  26.  
  27. void countsort(char *src, char *dest)
  28. {
  29.     int w = wcount(src);
  30.     char strings[w][20];
  31.     int i = 0, j;
  32.     char *tok = NULL;
  33.     //tok = strtok(src, " ");
  34.     tok = strtok(src, " ");
  35.     strncpy (strings[0], tok, 20);
  36.     //printf("%s\n",strings[0]);
  37.     int p = 1;
  38.     while (tok && p < w) {
  39.         //printf("Token: %s\n", tok);
  40.         tok = strtok(NULL, " ");
  41.         //p++;
  42.         strncpy(strings[p], tok, 20);
  43.         //printf("%s\n",strings[p]);
  44.         p++;
  45.     }
  46.     /*for (i = 0; i < p; i++) {
  47.         printf("strings[");
  48.         printf("%d", i);
  49.         printf("]=");
  50.         printf("%s \n", strings[i]);
  51.     }*/
  52.    
  53.     //printf("1\n");
  54.     //printf("%d \n", w);
  55.    
  56.     int count[w];
  57.     for (i = 0; i < w; i++) {
  58.         count[i] = 0;
  59.     }
  60.     int t1 = 0, t2 = 0;
  61.     for (j = 0; j < w - 1; j++) {
  62.         //printf("1");
  63.         for (i = 1 + j; i < w; i++) {
  64.             if (strlen(strings[i]) < strlen(strings[j])) {
  65.                 //t1 = strlen(strings[i]);
  66.                 //t2 = strlen(strings[j]);
  67.                 //printf("%d %d \n", t1, t2);
  68.                 count[j]++;
  69.                 //printf("count j++ = ");
  70.                 //printf("%d\n", j);
  71.             } else {
  72.                 count[i]++;
  73.                 //printf("count i++ = ");
  74.                 //printf("%d\n", i);
  75.             }
  76.         }
  77.     }
  78.    
  79.     /*for (i = 0; i < w; i++) {
  80.         printf("count[");
  81.         printf("%d", i);
  82.         printf("]=");
  83.         printf("%d \n", count[i]);
  84.     }*/
  85.    
  86.     for (i = 0; i < w; i++) {
  87.         for (j = 0; j < w; j++) {
  88.             if (count[j] == i) {
  89.                 strcpy(dest, strings[j]);
  90.                 dest += strlen(strings[j]);
  91.                 *dest++ = ' ';
  92.             }
  93.         }
  94.     }
  95. }
  96.  
  97. int main()
  98. {
  99.     char *src = malloc(100*sizeof(char));
  100.     char *dest = malloc(100*sizeof(char));
  101.     gets(src);
  102.     countsort(src, dest);
  103.     printf("%s ", dest);
  104.     free(dest);
  105.     free(src);
  106.     return 0;
  107. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement