c language strings partition function (rpm)

Source: C language split a string to a character segmentation

void Split ( char * src, const  char * separator, char ** dest, int * num) 
{ 
    / * 
        the first address of the source string src (buf address) 
        delimiting character separator specified 
        array dest received substrings 
        num division the number of the substring 
    * / 
     char * pNext;
      int COUNT = 0 ; 
     
     IF (the src == NULL || strlen (the src) == 0 ) // if the incoming address length is null or 0, terminate directly 
        return ; 
        
     IF (Separator == NULL || strlen (Separator) == 0 ) // not specified division string, terminated directly
        return ; 
        
     pNext = ( char *) strtok (src, Separator); // must use (char *) mandatory conversion (pointer error does not occur, although some do not write compiler) 
     the while (! pNext = NULL) {
           dest ++ = * pNext;
           ++ COUNT; 
         pNext = ( char *) strtok (NULL, Separator);   // must (char *) casts 
    }  
     * NUM = COUNT; 
}      
/ * 
int main () 
{ 
    int I ; 
    char buf [] = "www.baidu.com, www.taobao.com, www.csdn.com, www.python.org"; 
    // array for receiving return data. Sub-dividing ratio string array element number set here as long as a large enough.
    char * revbuf [8] = { 0}; // substring after storage divided
     
    number of the divided substrings @
    NUM = 0 int; 
    
    Split (buf, ",", revbuf, & NUM); // call the function is divided 
    
    
    for each content // returned output 
    for (I = 0; I <NUM; I ++) { 
        // lr_output_message ( "% S \ n-", revbuf [I]); 
        the printf ( "% S \ n-", revbuf [I]); 
    } 


    return 0; 
} * /

Dev c ++ in the results: 

    www.baidu.com 

    www.taobao.com 

    www.csdn.com 

    www.python.org

 

Guess you like

Origin www.cnblogs.com/LittleTiger/p/11328127.html