#include"stdio.h"#include"stdlib.h"#include"string.h"typedefintBOOL;#defineTRUE 1;#defineFALSE 0;Static voidSplitbyseparator (Char**arr,Char*STR,intSizeCharSep);voidSortnums (Char* STR,intSizeintCNT);intCOMPAREDIGSTR (Char* DIGSTR1,Char*digStr2);/*Extracts a string of numbers and sorts from strings, where: parameter: str string length, if < 0 is str ends with ' dest ', the buffer size that holds the number string is passed in the De The size of the St buffer and returns the length of the number string return value:-2 fetch failed, the size of the buffer holding the digital string is too small-1 fetch failed, the parameter is not valid 0 extraction failed, no number string >0 extract successfully, return Number of items back to the number string*/intExtractnums (Const Char* STR,intLengthChar* Dest,int*size) { intresult =-1; intSizedest =0;//Number String length intCntdest =0;//Number of items in the number string (cntdest+1)BOOL isneedsep = FALSE;//whether a delimiter is required Charch; if(str! = NULL && dest! = NULL && size! = NULL && *size >0) { if(Length <0)//str ends with ' /' { while(*str! =' /') {ch= *str; if(Ch >='0'&& CH <='9') { //Separators if(isneedsep) {if(Sizedest >= *size) {Result= -2;//fetch failed, the size of the buffer holding the number string is too small Break; } dest[sizedest++] =' ';//SeparatorsCntdest + +; Isneedsep=FALSE; } //Number Items if(Sizedest >= *size) {Result= -2;//fetch failed, the size of the buffer holding the number string is too small Break; } dest[sizedest++] =ch; } Else if(!isneedsep && sizedest >0) {Isneedsep= TRUE;//delimiter required before next number item} str++; } } Else { for(intI=0; i<length; i++) {ch=Str[i]; if(Ch >='0'&& CH <='9') { //Separators if(isneedsep) {if(Sizedest >= *size) {Result= -2;//fetch failed, the size of the buffer holding the number string is too small Break; } dest[sizedest++] =' ';//SeparatorsCntdest + +; Isneedsep=FALSE; } //Number Items if(Sizedest >= *size) {Result= -2;//fetch failed, the size of the buffer holding the number string is too small Break; } dest[sizedest++] =ch; } Else if(!isneedsep && sizedest >0) {Isneedsep= TRUE;//delimiter required before next number item } } } //output buffers are too small to be sorted if(Result! =-2) { *size = Sizedest;//length of the number string//number items are greater than 1 to sort if(++cntdest >1) {sortnums (dest, Sizedest, cntdest); Result= Cntdest;//returns the number of items in a number string } Else if(Sizedest! =0)//only one number item{result=cntdest; } Elseresult=0;//No number string } } returnresult;}/*string Ordering of numeric strings (small to Large), where: parameter: str numeric string size numeric string length CNT number string Item number return value: None*/voidSortnums (Char* STR,intSizeintCNT) { Char* * Arrstr = NULL;//Two-level pointers for storing segmented, extracted, and well-numbered items Char* strdest = NULL;//number of strings to be separated Char* Arrtmp =NULL; intNlen =0;//Number Item LengthBOOL issorted =FALSE; if(str! = NULL && size >0) {strdest= (Char*)malloc(size+1); if(Strdest! =NULL) {Arrstr= (Char**)malloc(cnt*sizeof(Char*));//apply CNT Pointer to save a numeric item address if(Arrstr! =NULL) {memcpy (strdest, str, size); * (strdest+size) =' /'; //Extracting numeric itemsSplitbyseparator (Arrstr, strdest, size,' '); //Bubble Sort for(intI=0; i<cnt-1; i++) {issorted=TRUE; //each round is compared before the cnt-1-i, that is, the last I of the sorted well without comparison for(intj=0; j<cnt-1-I.; J + +) { if(Comparedigstr (Arrstr[j], arrstr[j+1]) ==1) {issorted=FALSE; Arrtmp=Arrstr[j]; ARRSTR[J]= arrstr[j+1]; Arrstr[j+1] =arrtmp; } } if(issorted) Break;//No interchange has occurred, sort completed } //Str returns sort resultNlen = strlen (arrstr[0]); memcpy (str, arrstr[0], Nlen); STR+=Nlen; for(intI=1; i<cnt; i++) { *str =' '; STR++; Nlen=strlen (Arrstr[i]); memcpy (str, arrstr[i], nlen); STR+=Nlen; } Free(ARRSTR); } Free(strdest); } }}/*Extract a number item (static static function), where: parameter: Arr Returns the number of extracted items str number string size number string length Sep delimiter return value: None*/voidSplitbyseparator (Char**arr,Char*STR,intSizeCharSep) { Char* PEnd = str +size; for(*arr = str; str < pEnd; str++) if(*str = =Sep) { *str =' /'; * (++arr) = str +1; }}/*Compare number string size, where: parameter: digStr1 number string 1 digStr2 number string 2 return value: <0 digStr1 less than digStr2 0 digStr1 equals digStr2 > 0 digStr1 greater than DIGSTR2*/intCOMPAREDIGSTR (Char* DIGSTR1,Char*digStr2) { intresult =-1; intDig1len =0; intDig2len =0; if(digStr1! = NULL && DIGSTR2! =NULL) { //except for the digital string head ' 0 ' while(*DIGSTR1 = ='0') {digStr1++; } while(*DIGSTR2 = ='0') {digStr2++; } Dig1len=strlen (DIGSTR1); Dig2len=strlen (DIGSTR2); if(Dig1len >Dig2len) {Result=1; } Else if(Dig1len = =Dig2len) {Result=strncmp (DIGSTR1, DIGSTR2, Dig1len); } } returnresult;}
Extracting numeric strings from a string and sorting them (C language implementation)