SORU
9 ŞUBAT 2012, PERŞEMBE


C sınırlayıcıları ile dize bölmek

Nasıl bölmek için bir fonksiyon yazın ve C programlama dilinde sınırlayıcı bir dize için bir dizi dönmek?

char* str = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
str_split(str,',');

CEVAP
9 ŞUBAT 2012, PERŞEMBE


strtok() bir dize (ve sınırlayıcı belirtin) split fonksiyonunu kullanabilirsiniz. strtok() dize geçirilen değiştirmek unutmayın. Eğer özgün dize gerekli ise, başka bir yerde bir kopyasını alın ve strtok() kopyala geçirir.

DÜZENLEME:

(Ardışık ayırıcıları işlemez not, "OCA, ŞUB," örneğin) MAR . örnek

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <assert.h>

char** str_split(char* a_str, const char a_delim)
{
    char** result    = 0;
    size_t count     = 0;
    char* tmp        = a_str;
    char* last_comma = 0;
    char delim[2];
    delim[0] = a_delim;
    delim[1] = 0;

    /* Count how many elements will be extracted. */
    while (*tmp)
    {
        if (a_delim == *tmp)
        {
            count  ;
            last_comma = tmp;
        }
        tmp  ;
    }

    /* Add space for trailing token. */
    count  = last_comma < (a_str   strlen(a_str) - 1);

    /* Add space for terminating null string so caller
       knows where the list of returned strings ends. */
    count  ;

    result = malloc(sizeof(char*) * count);

    if (result)
    {
        size_t idx  = 0;
        char* token = strtok(a_str, delim);

        while (token)
        {
            assert(idx < count);
            *(result   idx  ) = strdup(token);
            token = strtok(0, delim);
        }
        assert(idx == count - 1);
        *(result   idx) = 0;
    }

    return result;
}

int main()
{
    char months[] = "JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC";
    char** tokens;

    printf("months=[%s]\n\n", months);

    tokens = str_split(months, ',');

    if (tokens)
    {
        int i;
        for (i = 0; *(tokens   i); i  )
        {
            printf("month=[%s]\n", *(tokens   i));
            free(*(tokens   i));
        }
        printf("\n");
        free(tokens);
    }

    return 0;
}

Çıkış:

$ ./main.exe
months=[JAN,FEB,MAR,APR,MAY,JUN,JUL,AUG,SEP,OCT,NOV,DEC]

month=[JAN]
month=[FEB]
month=[MAR]
month=[APR]
month=[MAY]
month=[JUN]
month=[JUL]
month=[AUG]
month=[SEP]
month=[OCT]
month=[NOV]
month=[DEC]

Bunu Paylaş:
  • Google+
  • E-Posta
Etiketler:

YORUMLAR

SPONSOR VİDEO

Rastgele Yazarlar

  • GOTO Conferences

    GOTO Confere

    3 EKİM 2011
  • Nickcidious

    Nickcidious

    6 HAZİRAN 2011
  • TheMasterOfHell100

    TheMasterOfH

    13 AĞUSTOS 2011