Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- //
- // countsort.c
- //
- //
- // Created by MacBook return 0; on 08.11.14.
- //
- //
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- int wcount(char *s)
- {
- int k, count = 0;
- int len=strlen(s);
- for (k = 0; k < len; k++) {
- if (s[k]!=' '){
- count++;
- do k++; while ((k < len) && (s[k]!=' ') && (s[k]!='\0') && (s[k]!='\t'));
- }
- }
- return count;
- }
- void countsort(char *src, char *dest)
- {
- int w = wcount(src);
- char strings[w][20];
- int i = 0, j;
- char *tok = NULL;
- //tok = strtok(src, " ");
- tok = strtok(src, " ");
- strncpy (strings[0], tok, 20);
- //printf("%s\n",strings[0]);
- int p = 1;
- while (tok && p < w) {
- //printf("Token: %s\n", tok);
- tok = strtok(NULL, " ");
- //p++;
- strncpy(strings[p], tok, 20);
- //printf("%s\n",strings[p]);
- p++;
- }
- /*for (i = 0; i < p; i++) {
- printf("strings[");
- printf("%d", i);
- printf("]=");
- printf("%s \n", strings[i]);
- }*/
- //printf("1\n");
- //printf("%d \n", w);
- int count[w];
- for (i = 0; i < w; i++) {
- count[i] = 0;
- }
- int t1 = 0, t2 = 0;
- for (j = 0; j < w - 1; j++) {
- //printf("1");
- for (i = 1 + j; i < w; i++) {
- if (strlen(strings[i]) < strlen(strings[j])) {
- //t1 = strlen(strings[i]);
- //t2 = strlen(strings[j]);
- //printf("%d %d \n", t1, t2);
- count[j]++;
- printf("count j++ = ");
- printf("%d\n", j);
- } else {
- count[i]++;
- printf("count i++ = ");
- printf("%d\n", i);
- }
- }
- }
- for (i = 0; i < w; i++) {
- printf("count[");
- printf("%d", i);
- printf("]=");
- printf("%d \n", count[i]);
- }
- for (i = 0; i < w; i++) {
- for (j = 0; j < w; j++) {
- if (count[j] == i) {
- strcpy(dest, strings[j]);
- dest += strlen(strings[j]);
- *dest++ = ' ';
- }
- }
- }
- /*int y = 0;
- for (i = 0; i < w; i++) {
- y += strlen(strings[i]);
- y++;
- }*/
- //memset(dest, 0, y);
- *(--dest) = 0;
- }
- int main()
- {
- char *src = malloc(100*sizeof(char));
- char *dest = malloc(100*sizeof(char));
- gets(src);
- countsort(src, dest);
- printf("%s\n", dest);
- free(src);
- free(dest);
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement