Advertisement
Guest User

Untitled

a guest
May 26th, 2019
125
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C 3.35 KB | None | 0 0
  1. #include <stdio.h>
  2. #include <stdlib.h>
  3. #include <string.h>
  4. #include <ctype.h>
  5. #include "tested_declarations.h"
  6. #include "rdebug.h"
  7.  
  8. int count_words(const char* ptr)
  9. {
  10.     int i = 0, counter = 0;
  11.     for(i = 0; *(ptr+i) != '\0'; ++i)
  12.     {
  13.         if( (*(ptr+i)>='A' && *(ptr+i)<='Z')  || (*(ptr+i)>='a' && *(ptr+i)<='z') ) {
  14.             if( !((*(ptr+i+1)>='a' && *(ptr+i+1)<='z')|| (*(ptr+i+1)>='A' && *(ptr+i+1)<='Z'))) {
  15.                 counter += 1;
  16.             }
  17.         }
  18.     }
  19.     return counter;
  20. }
  21. void destroy(char **words)
  22. {
  23.     if(words == NULL) return ;
  24.    
  25.     int i=0;
  26.     while(*(words+i)!=NULL)
  27.     {
  28.         free(*(words+i));
  29.         i++;
  30.     }
  31.     free(words);
  32. }
  33.  
  34. char **split_words(const char *text)
  35. {
  36.     int size = count_words(text);
  37.     char** ptr = (char **)malloc(sizeof(char *)*(size+1));
  38.     if(!ptr) return NULL;
  39.     if(size == 0) { free(ptr); return NULL; }
  40.     int counter = 0, counter2 = 0, counter3 = 0;
  41.     int i = 0, j = 0;
  42.  
  43.     for(i = 0; *(text+i) != '\0'; i++)
  44.     {
  45.         if((*(text+i)>='a' && *(text+i)<='z')|| (*(text+i)>='A' && *(text+i)<='Z')) {
  46.             if(!((*(text+i+1)>='a' && *(text+i+1)<='z')|| (*(text+i+1)>='A' && *(text+i+1)<='Z'))) {
  47.                 *(ptr+counter)=(char *)malloc(sizeof(char)*(counter2+2));
  48.                 if(!(*(ptr+counter))) {
  49.                     j = 0;
  50.                     while(j < counter) { free(*(ptr+j)); j++; }
  51.                     free(ptr);
  52.                     return NULL;
  53.                 }
  54.                 counter++;
  55.                 counter2=0;
  56.             }
  57.             else counter2++;
  58.         }
  59.     }
  60.     *(ptr+counter)=NULL;
  61.    
  62.      
  63.     for(i = 0, counter = 0; *(text+i) != '\0' ; i++)
  64.     {
  65.         if((*(text+i)>='a' && *(text+i)<='z')|| (*(text+i)>='A' && *(text+i)<='Z'))
  66.         {
  67.             *(*(ptr+counter)+counter3)=*(text+i);      
  68.             if( !((*(text+i+1)>='a' && *(text+i+1)<='z')|| (*(text+i+1)>='A' && *(text+i+1)<='Z')))
  69.             {
  70.                 *(*(ptr+counter)+counter3+1)='\0';
  71.                 counter++;
  72.                 counter3=0;
  73.             }
  74.             else  counter3++;
  75.         }
  76.     }
  77.     return ptr;
  78. }
  79. void swap(char **a, char **b)
  80. {
  81.     char* temp;
  82.     temp = *a;
  83.     *a = *b;
  84.     *b = temp;
  85.    
  86. }
  87. int sort_words(char **words)
  88. {
  89.     if(!words || !(*words)) return 1;
  90.     for(int i = 0; *(words+i); ++i)
  91.     {
  92.         for(int j = 0; *(words+j+1); ++j)
  93.         {
  94.             if(strcmp(*(words+j),*(words+j+1)) > 0 ) {
  95.                 swap((words+j), (words+j+1));
  96.             }
  97.         }
  98.     }  
  99.     return 0;
  100. }
  101. void show(char ** text)
  102. {
  103.     while(*text != NULL) {
  104.         printf("%s \n",*text);
  105.         text++;
  106.     }
  107. }
  108.  
  109. int main()
  110. {
  111.     int var = 0;
  112.     char* ptxt = (char *)malloc(sizeof(char)*1000);
  113.     if(!ptxt) { printf("Failed to allocate memory"); return 8; }
  114.     printf("Enter: ");
  115.     fgets(ptxt, 1000, stdin);
  116.     for(int i = 0; *(ptxt+i) != '\0'; i++) {
  117.         if((*(ptxt+i)>='A' && *(ptxt+i)<='Z') || (*(ptxt+i)>='a' && *(ptxt+i)<='z')) var++;
  118.     }
  119.     if(var==0) { printf("Nothing to show"); free(ptxt); return 0; }
  120.  
  121.    
  122.     char ** ptr_ret = split_words(ptxt);
  123.    
  124.     if(!ptr_ret) { free(ptxt); printf("Failed to allocate memory"); return 8; }
  125.     sort_words(ptr_ret);
  126.     show(ptr_ret);
  127.     free(ptxt);
  128.     destroy(ptr_ret);
  129.     return 0;
  130. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement