Dynamically allocating memory sort any string of input #include<stdio.h> #include <stdlib.h> #include <malloc.h> #include <string.h >//Comparison size//int str_compare (const char* src,const char* DST)//{//int ret = 0;//if (src = = DST)//{//return 0;//}//if (DST = = NULL)//{//return-1;//}//while (! ( RET = * (unsigned char*) src-* (unsigned char*) DST) && *dst)//{//++src,++dst;//}//if (Ret < 0)//{//ret = -1;//}// else if (Ret > 0)//{//ret = 1;//}//return ret;//}//sort function void sort_string (char *strs[],int len) {int i=0,j;for (i=0;i<l en;i++) {for (j = 1; j < Len-i; J + +) {if (strcmp (Strs[j-1],strs[j]) >0) {char *tmp = 0;tmp = Strs[j-1];strs[j-1] = St RS[J];STRS[J] = tmp;}}} printf ("\ n output sorted string: \ n");//output sorted string for (i=0;i<len;i++) {printf ("%s\n", Strs[i]);}} int main () {char *str=null,**strs=null,*p;unsigned int str_size=0,strs_size=0,i;printf ("Please Enter string: (String separated by space, enter end input) \ n ");d o{//dynamically allocates memory char *tmp= (char*) realloc (str,++str_size*sizeof (char)), if (!tmp) {free (str); Fputs (" Out of Memory ", STDERR); return 1;} str=tmp;str[str_sIze-1]=getchar ();} while (str[str_size-1]!= ' \ n '); Str[str_size-1]=0;for (P=strtok (str, "\ t");p; P=strtok (NULL, "\ T")) {char **tmp= (char** ) ReAlloc (strs,++strs_size*sizeof (char*)), if (!tmp) {free (str); free (STRs); Fputs ("Out of Memory", stderr); return 1;} Strs=tmp;strs[strs_size-1]=p;} printf ("\ nthe string you typed is: \ n"), for (i=0;i<strs_size;++i) printf ("%u:%s\n", i+1,strs[i]);//Sort sort_string (Strs,strs_ size);//Release Memory free (STRs); (str); system ("pause"); return 0;}
C language-Dynamically allocating memory to sort any of the strings