C language-Dynamically allocating memory to sort any of the strings

Source: Internet
Author: User

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

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.