Split string with delimiters in C

前端 未结 20 1416
你的背包
你的背包 2020-11-21 11:56

How do I write a function to split and return an array for a string with delimiters in the C programming language?

char* str = \"JAN,FEB,MAR,APR,MAY,JUN,JUL,         


        
20条回答
  •  难免孤独
    2020-11-21 12:15

    This is a string splitting function that can handle multi-character delimiters. Note that if the delimiter is longer than the string that is being split, then buffer and stringLengths will be set to (void *) 0, and numStrings will be set to 0.

    This algorithm has been tested, and works. (Disclaimer: It has not been tested for non-ASCII strings, and it assumes that the caller gave valid parameters)

    void splitString(const char *original, const char *delimiter, char ** * buffer, int * numStrings, int * * stringLengths){
        const int lo = strlen(original);
        const int ld = strlen(delimiter);
        if(ld > lo){
            *buffer = (void *)0;
            *numStrings = 0;
            *stringLengths = (void *)0;
            return;
        }
    
        *numStrings = 1;
    
        for(int i = 0;i < (lo - ld);i++){
            if(strncmp(&original[i], delimiter, ld) == 0) {
                i += (ld - 1);
                (*numStrings)++;
            }
        }
    
        *stringLengths = (int *) malloc(sizeof(int) * *numStrings);
    
        int currentStringLength = 0;
        int currentStringNumber = 0;
        int delimiterTokenDecrementCounter = 0;
        for(int i = 0;i < lo;i++){
            if(delimiterTokenDecrementCounter > 0){
                delimiterTokenDecrementCounter--;
            } else if(i < (lo - ld)){
                if(strncmp(&original[i], delimiter, ld) == 0){
                    (*stringLengths)[currentStringNumber] = currentStringLength;
                    currentStringNumber++;
                    currentStringLength = 0;
                    delimiterTokenDecrementCounter = ld - 1;
                } else {
                    currentStringLength++;
                }
            } else {
                currentStringLength++;
            }
    
            if(i == (lo - 1)){
                (*stringLengths)[currentStringNumber] = currentStringLength;
            }
        }
    
        *buffer = (char **) malloc(sizeof(char *) * (*numStrings));
        for(int i = 0;i < *numStrings;i++){
            (*buffer)[i] = (char *) malloc(sizeof(char) * ((*stringLengths)[i] + 1));
        }
    
        currentStringNumber = 0;
        currentStringLength = 0;
        delimiterTokenDecrementCounter = 0;
        for(int i = 0;i < lo;i++){
            if(delimiterTokenDecrementCounter > 0){
                delimiterTokenDecrementCounter--;
            } else if(currentStringLength >= (*stringLengths)[currentStringNumber]){
                (*buffer)[currentStringNumber][currentStringLength] = 0;
                delimiterTokenDecrementCounter = ld - 1;
                currentStringLength = 0;
                currentStringNumber++;
            } else {
                (*buffer)[currentStringNumber][currentStringLength] = (char)original[i];
                currentStringLength++;
            }
        }
        buffer[currentStringNumber][currentStringLength] = 0;
    }
    

    Sample code:

    int main(){
        const char *string = "STRING-1 DELIM string-2 DELIM sTrInG-3";
        char **buffer;
        int numStrings;
        int * stringLengths;
    
        splitString(string, " DELIM ", &buffer, &numStrings, &stringLengths);
    
        for(int i = 0;i < numStrings;i++){
            printf("String: %s\n", buffer[i]);
        }
    }
    

    Libraries:

    #include 
    #include 
    #include 
    

提交回复
热议问题